简体   繁体   English

ASP.NET Webform css链接被破坏

[英]ASP.NET Webform css link getting mangled

For some reason by css link in a webforms master page is getting mangled by ASP.NET. 出于某种原因, webforms母版页中的css链接被ASP.NET破坏了。

The page using the masterpage is located in /subdir1/subdir2/page.aspx 使用母版页的页面位于/subdir1/subdir2/page.aspx中

Not sure why it is happening but here is a snippet of the code: 不知道为什么会发生这种情况,但这里有一段代码:

<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>

The Html output that is being created is: 正在创建的Html输出是:

<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 is this working for the script tag but mangling the link tag and not actually executing the code included. 为什么这适用于脚本标记,但是会破坏链接标记而不是实际执行包含的代码。 If I change the 'link' tag to be a 'script' tag (which is wrong but for testing purposes) it produces the proper html I would expect. 如果我将'link'标记更改为'script'标记(这是错误的但是出于测试目的),它会生成我期望的正确html。 Why is ASP.NET messing with my link tag for my Css but not the script tag for the javascript? 为什么ASP.NET弄乱我的Css的链接标签而不是javascript的脚本标签?

Is there something special about the link tag to make ASP.NET think it needs to mangle it? 是否有一些特殊的关于标记,以使ASP.NET认为它需要破坏它?

This is a separate answer based on approach and might be more what you are looking for. 这是一个基于方法的单独答案,可能更符合您的要求。 The reason I found for the string mangling is the HtmlLink object has internal handling of the href value during rendering. 我发现字符串重整的原因是HtmlLink对象在渲染过程中内部处理了href值。 Using .NET Reflector I found an Overrides RenderAttributes method. 使用.NET Reflector我发现了一个Overrides RenderAttributes方法。 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

What I believe is happening is the RenderAttributes method is being called before your helper line is being parsed and is using ResolveClientUrl against the string "<%= MyNamespace.Helpers.UrlHelper.CssRoot %>Site.css". 我相信正在发生的是在解析辅助线之前调用RenderAttributes方法,并且对字符串“<%= MyNamespace.Helpers.UrlHelper.CssRoot%> Site.css”使用ResolveClientUrl。 The solution of using "~/" URL strings isn't affected by this because ResolveClientUrl is able to understand that notation. 使用“〜/”URL字符串的解决方案不受此影响,因为ResolveClientUrl能够理解该表示法。

I see two solutions for you at this point. 我现在看到两个解决方案。 1) Use the Edit #2 approach of injecting the helper's URL string into the element during Page_Load or Page_PreRender, or 2) Create your own Overrriden version of the HtmlLink element that doesn't try to use ResolveClientUrl. 1)使用Edit#2方法在Page_Load或Page_PreRender期间将帮助程序的URL字符串注入元素,或2)创建自己的HtmlLink元素的Overrriden版本,该版本不尝试使用ResolveClientUrl。 #1 would definitely be the easier solution. #1绝对是更容易的解决方案。

Hope this helps shed some light on the issue. 希望这有助于阐明这个问题。

Perhaps a solution would be to specify your <link> and <script> tags from your master page's codebehind. 也许解决方案是从主页的代码隐藏中指定<link><script>标记。

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));
}

It was not wise for ASP.NET team to make link tags auto-adjust their href attributes, but the best way I've found to deal with it is to: ASP.NET团队使链接标记自动调整其href属性并不明智,但我发现处理它的最佳方法是:

  1. Dynamically insert the link tag into a Literal control. 将链接标记动态插入Literal控件。
  2. Remove the runat="server" from the head tag. 从head标签中删除runat =“server”。

It may be a bit hacky, but gets the problem solved, and looks "somewhat" like regular html. 它可能有点hacky,但解决了问题,看起来像“常规html”。

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

Note that in this example I am u sing T4MVC. 请注意,在这个例子中,我是T4MVC。 As posted by Chris Porter, the problem is href . 由Chris Porter发布,问题是href By calling the Response.Write you get around that. 通过调用Response.Write,你可以解决这个问题。 Enjoy! 请享用!

Try runat="server" tags in the elements 在元素中尝试runat =“server”标记

<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 have to use the helper object? 编辑:你必须使用帮助对象吗? Is it doing more than just making your links dynamic? 它不只是让你的链接动态吗? When runat="server" is set, you can sometimes use dynamic URLs using the ~ character. 当设置runat =“server”时,您有时可以使用〜字符使用动态URL。 You could try this instead: 你可以试试这个:

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

Edit #2: Try injecting the URL into the link element from the Page_Load or Page_PreRender events in the code behind of the page. 编辑#2:尝试将URL从页面后面的代码中的Page_Load或Page_PreRender事件注入link元素。 You will need the element to have runat="server" for this to work. 您将需要该元素具有runat =“server”才能使其正常工作。

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

You could try changing 你可以尝试改变

<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 on the second instance 请注意第二个实例上的单引号

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM