ASP.NET Webform css link crippled

For some reason, the css link on the main web form page is becoming garbled by ASP.NET.

The page using the main page is located in / subdir 1 / subdir2 / page.aspx

Not sure why this is happening, but here is the code snippet:

<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <link href="<%= MyNamespace.Helpers.UrlHelper.CssRoot %>Site.css" rel="stylesheet" type="text/css" /> <script src="<%= MyNamespace.Helpers.UrlHelper.JavascriptRoot %>jquery-1.3.2.min.js" type="text/javascript"></script> <asp:ContentPlaceHolder ID="cphHead" runat="server"> </asp:ContentPlaceHolder> </head> 

Generated HTML output:

 <html xmlns="http://www.w3.org/1999/xhtml" > <head><title> Untitled Page </title><link href="../../%3C%25=%MyNamespace.Helpers.UrlHelper.CssRoot%20%25%3ESite.css" rel="stylesheet" type="text/css" /> <script src="/Javascript/jquery-1.3.2.min.js" type="text/javascript"></script> </head> 

Why does this work for a script tag, but it distorts the link tag and does not actually execute the code. If I change the 'link' tag as a 'script' tag (this is wrong, but for testing purposes), it creates the correct html that I would expect. Why is ASP.NET fiddling with its link tag for my Css but not the javascript script tag?

Is there anything special about the link tag so that ASP.NET thinks it needs to use it?

+3
c # webforms
source share
6 answers

This is a separate answer based on an approach and perhaps more than what you are looking for. The reason I found that the hgmlLink object has an internal processing of the href value during rendering. Using the .NET Reflector, I found the Overrides RenderAttributes method. This is the code for it:

 Protected Overrides Sub RenderAttributes(ByVal writer as HtmlTextWriter) If Not String.IsNullOrEmpty(Me.Href) Then MyBase.Attributes.Item("href") = MyBase.ResolveClientUrl(Me.Href) End If MyBase.RenderAttributes(writer) End Sub 

I believe the RenderAttributes method is called before your syntax string is parsed and uses ResolveClientUrl for the string "<% = MyNamespace.Helpers.UrlHelper.CssRoot%> Site.css". The decision to use the URL strings "~ /" is independent of this, because ResolveClientUrl can understand this notation.

I see two solutions for you at this stage. 1) Use the Edit # 2 approach to enter the sub-URL string into the element during Page_Load or Page_PreRender or 2) Create your own Overrriden version of the HtmlLink element that does not attempt to use ResolveClientUrl. # 1 will definitely be an easier solution.

Hope this helps shed light on the issue.

+3
source share

Perhaps the solution would be to specify the <link> and <script> from your main page code.

 private void ConstructLinkAndScriptTags() { string cssTag = "<link href='" + MyNamespace.Helpers.UrlHelper.CssRoot + "Site.css' rel='stylesheet' type='text/css' runat='server' />"; cph.Controls.Add(new LiteralControl(cssTag)); } 
+1
source share

Try runat = "server" tags in elements

 <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <link runat="server" href="<%= MyNamespace.Helpers.UrlHelper.CssRoot %>Site.css" rel="stylesheet" type="text/css" /> <script runat="server" src="<%= MyNamespace.Helpers.UrlHelper.JavascriptRoot %>jquery-1.3.2.min.js" type="text/javascript"></script> <asp:ContentPlaceHolder ID="cphHead" runat="server"> </asp:ContentPlaceHolder> </head> 

Edit: Do you need to use a helper object? Is this more than just making your links dynamic? When runat = "server" is installed, sometimes you can use dynamic URLs using the ~ character. Instead, you can try:

 <head runat="server"> <link id="SiteCssLink" runat="server" href="~/Css/Site.css" rel="Stylesheet" type="text/css" media="all" /> </head> 

Edit # 2: Try entering the URL into the link element from the Page_Load or Page_PreRender events in the code located behind the page. To do this, you need the runat = "server" element.

 Protected Sub Page_PreRender(ByVal sender as Object, ByVal e as System.EventArgs) Handles Me.PreRender SiteCssLink.Href = Page.ResolveClientUrl(String.Concat(MyNamespace.Helpers.UrlHelper.CssRoot, "Site.css")) End Sub 
0
source share

You can try to change

 <link href="<%= MyNamespace.Helpers.UrlHelper.CssRoot %>Site.css" rel="stylesheet" type="text/css" /> 

to

 <link href='<%= MyNamespace.Helpers.UrlHelper.CssRoot %>Site.css' rel="stylesheet" type="text/css" /> 

Note the single quotes in the second instance

0
source share

It was impractical for the ASP.NET team to create link tags, automatically adjust their href attributes, but the best way I found using this is:

  • Dynamically insert a link tag into a Literal control.
  • Remove runat = "server" from the main tag.
0
source share

This may be a bit hacky, but the problem is resolved and looks "somewhat" like normal html.

 <link <% this.Response.Write( "href=\"" + Links.Content.Site_css + "\""); %> rel="stylesheet" type="text/css" /> 

Please note that in this example I am drinking T4MVC. According to Chris Porter, the problem is in href. By calling Response.Write, you get around this. Enjoy it!

0
source share

All Articles