Inline code in head tag – ASP.NET

Is it possible to do something like this in a head tag, of master page, which has runatserver:

 <link rel="Stylesheet" type="text/css" href='<%=Config.ResourcesDomain %>/images/style.css' />

This is not working, as it produces this kind of html:

<link rel="Stylesheet" type="text/css" href="&lt;%=Config.ResourcesDomain %>/images/style.css" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" />

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The reason the output is being rendered like so:

href="&lt;%=Config.ResourcesDomain %>/images/style.css" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"

Is because ASP.NET is treating the link as an HtmlLink control, and rendering the contents of the href attribute as a literal.

This is a strange quirk of marking the head section as a server control, where certain elements are treated as server controls (even without being marked explicitly with the runat="server" attribute).

Removing the quotations around the href attribute resolves the issue:

href=<%= Config.ResourcesDomain %>/images/style.css

Doing so stops the link element being treated as a server control, thus executing the code block and rendering the correct URL.

However, the above writes the href value out without quotes. Using the following, will add the quotes to the link tag:

href=<%= String.Format("'{0}'", Config.ResourcesDomain) %>/images/style.css

Hope this helps.

Edit

Strangely, if you use double quotes for the href attribute, and include double quotes within the code block this also resolves the issue:

href="<%= " rel="nofollow noreferrer noopener"" + Config.ResourcesDomain %>/images/style.css"

However, none of the above are particularly elegant solutions, and setting the URL from the code behind is probably the way to go.

Method 2

Another solution I’ve found here: https://stackoverflow.com/a/5727996/368613
just place code inside PlaceHolder:

<asp:PlaceHolder runat="server">
  ... your code with <%= %> tags ...
</asp:PlaceHolder>

Method 3

mark-up

<head>
   <asp:Literal ID="litHead" runat="server" />
</head>

code-behind:

on page_load

litHead.Text = "<link rel='Stylesheet'  type='text/css' href='" + Config.ResourcesDomain + "/images/style.css' />";

Update:
do this then

<head runat="server">
    <%
        Response.Write("<link rel='Stylesheet'  type='text/css' href='" + Config.ResourcesDomain + "/images/style.css' />");
    %>
    <title></title>
</head>

Method 4

change it to

 <link rel="Stylesheet"  type="text/css" href='<%Response.Write(Config.ResourcesDomain); %>/images/style.css' />

It should work

Method 5

Remove the runat=”server” attribute on the opening head tag.
This way the asp.net inline code is correctly rendered.

Method 6

Or do something like this:

<head>
    <style type="text/css">
        @import "<%= ResolveUrl("~/content/styles.css") %>";
        @import "<%= ResolveUrl("~/content/print.css") %>" print;
    </style>
</head>

Method 7

Apparently data binding is required when using the inline tag “<%# %>”.

<head id="Head">
    //Stuff with inline code
</head>

Code Behind:

protected void Page_Load {
    Head.DataBind();
}

Regards


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x