开发者

ASP.NET MVC inline tags not working with <link href>

Every time I put an inline tag on the link's href attribute it somehow gets auto-encoded. Is this ASP.NET's default behavior? How can I dynamically set the Href attribute using code? This is in MVC btw.

Attempted something like this

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

which rendered this (nothing changed)

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

and this

<link href="<%= Link.Content.Jquery_css %> rel="stylesheet" type="text/css" />
开发者_如何学编程

which produced this (I couldn't remember the exact numbers, but it seems the bracket-percent-equals was encoded to link format)

<link href="/View/Shared%25Link.Content.Jquery_css%25" %>" rel="stylesheet" type="text/css" />

Link.Content.Jquery_css is a strongly typed string containing the link made using T4MVC.

Add'l info: I used ASP.NET MVC 2, in .NET 4 and testing in Firefox.


I got the same issue in the master page. (It doesn't happen in an individual page.) I found removing "runat=server" from the head tag fixed the problem.


It's getting auto-encoded because of the tag your using (<%: %>). If you don't want the URL to be Encoded, use the following:

<link href="<%= Link.Content.Jquery_css %>" rel="stylesheet" type="text/css" />


Change the ":" to "=" and that remove the auto encoding


Your view is unable to access Link.Content.Jquery_css property. ASP.NET unhelpfully doesn't throw any error.

Move that line inside the body of the page and you will see compilation error.


You could do this:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜