Why is my code not evaluated as expected inside of a tag attribute?
This code:
<% string path = Request.ApplicationPath.ToString(); %>
<link href="<%= path %>/Content/Site.css" rel="stylesheet" type="text/css" />
Returns this:
<link href="../Views/Shared/%3C%25=%20path%20%25%3E/Content/Site.css" rel="stylesheet" type="text/css" />
Whereas I was expecting this:
<link href="/foo/Content/Site.css" rel="stylesheet" type="text/css" />
Why did my code not return the expected path? How do I set up my scripts, css fi开发者_如何学运维les, and images to be flexible if my virtual directory changes?
To answer your first question, what view engine are you using? Are you using MVC3, which I believe defaults to Razor and not Asp.Net for the view engine?
To answer your second question, you should try this:
<link href="<%: Url.Content( "~/Content/Site.css" ) %>" rel="stylesheet" type="text/css" />
That should output what you want
Virtual paths can be specified using the ~ (tilde) prefix.
精彩评论