开发者

Is this a bug? Or is it a setting in ASP.NET 4 (or MVC 2)?

I just recently started trying out T4MVC and I like the idea of eliminating magic strings.

However, when trying to use it on my master page for my stylesheets, I get this:

<link href="<%: Links.Content.site_css %>" rel="stylesheet" type="text/css" />

rending like this:

<link href="&lt;%: Links.Content.site_css %>" rel="stylesheet" type="text/css" />

Whereas these render correctly:

<link href="<%: Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />
<link href="<%: Links.Content.site_css + "" %>" rel="stylesheet" type="text/css" />

It appears that, as long as I have double quotes inside of the code segment, it works. But when I put anything else in there, it escapes the leading "less than".

Is this something I can 开发者_如何学Pythonturn off? Is this a bug?


Edit:

This does not happen for <script src="..." ... />, nor does it happen for <a href="...">.


Edit 2:

Minimal case:

<link href="<%: string.Empty %>" />

vs

<link href="<%: "" %>" />


Edit 3:

I have a workaround, I have implemented an HtmlHelper extension so that I can do this:

<%: Html.StyleSheet(Links.Content.site_css) %>

I like the support for intellisens better, so I'm actually going to stick with that. Right now, I'm just trying to solve the bug.


It looks like a bug to me, the compiled output for that is:

private global::System.Web.UI.HtmlControls.HtmlLink @__BuildControl__control5() {
    global::System.Web.UI.HtmlControls.HtmlLink @__ctrl;            
    @__ctrl = new global::System.Web.UI.HtmlControls.HtmlLink();
    @__ctrl.Href = "<%: String.Empty %>";
    ((System.Web.UI.IAttributeAccessor)(@__ctrl)).SetAttribute("rel", "stylesheet");
    ((System.Web.UI.IAttributeAccessor)(@__ctrl)).SetAttribute("type", "text/css");
    return @__ctrl;
}

This seems to only happen when the control is inside a head runat="server"


I think the problem is the colon. Try <link href="<%= string.Empty %>" /> instead of <link href="<%: string.Empty %>" />.


According to the Gu, <%: is a feature of ASP.NET 4.0. Try targeting 4.0 and see if its still failing.


Remove the runat="server" attribute from the head tag.

The server generated head tag will render a HtmlLink tag for any link tag. The content of href is taken from source and urlencoded. This is not done for script includes.

EDIT:
Write your link tags like following if you cant remove the runat="server" from head tag:

<link href="<%= ... %>" runat="" />

The following gives an error "runat attribute must have value server"

<link href="<%= ... %>myfile.css" runat="" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜