Reputation: 12281
I am working in an ASP.NET MasterPage and am having trouble with <link href="..." />
.
I am trying to substitute in a stylesheet with a specific name:
<link href="/Content/Styles/<%=Model.Style%>.css" rel="stylesheet" type="text/css" />
Unfortunately, this creates the HTML output:
<link href="/Content/Styles/<%=Model.Style%>.css" rel="stylesheet" type="text/css" />
Which is clearly not what was intended.
If I put the same code in a View placeholder, it works perfectly. This is not a good solution though as I have many pages where I just want it to do the same thing.
It looks like it's trying to automatically correct the URL - is there a way to switch this off?
I have fixed this temporarily using:
<link href=<%=String.Format("\"/Content/Styles/{0}.css\"", Model.Style)%> rel="stylesheet" type="text/css" />
Upvotes: 2
Views: 152
Reputation: 126547
All the links in your question and in the solution posted thus far will fail if your site is deployed in a virtual folder. Instead, do:
<link href="<%= Url.Content("~/Content/Styles/" + Model.Style + ".css") %>" rel="stylesheet" type="text/css" />
This (1) fixes the problem in your question, and (2) allows your site to work in a virtual folder.
Upvotes: 1
Reputation: 1038890
Try this:
<link href="/Content/Styles/<%= "" + Model.Style%>.css" rel="stylesheet" type="text/css" media="screen" />
Ugly but works.
Upvotes: 0