Ryall
Ryall

Reputation: 12281

Unable to generate a custom path in ASP.NET MVC Master Pages

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" />

Upvotes: 2

Views: 152

Answers (2)

Craig Stuntz
Craig Stuntz

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

Darin Dimitrov
Darin Dimitrov

Reputation: 1038890

Try this:

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

Ugly but works.

Upvotes: 0

Related Questions