views:

84

answers:

2

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/&lt;%=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?


Edit 1:

I have fixed this temporarily using:

<link href=<%=String.Format("\"/Content/Styles/{0}.css\"", Model.Style)%> rel="stylesheet" type="text/css" />
A: 

Try this:

<link href="/Content/Styles/<%= "" + Model.Style%>.css" rel="stylesheet" type="text/css" media="screen" />

Ugly but works.

Darin Dimitrov
+1  A: 

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.

Craig Stuntz
Nice one, cheers.
Nat Ryall