Nilzor
Nilzor

Reputation: 18603

How to set stylesheet programmatically in an ASP.NET MVC 2 project?

So.. this is the upper part of my site.master ASPX file in an MVC2 project:

<head runat="server">
   <link href="<%= ViewData[SomeNamespace.StyleSheetKey]; %>" rel="stylesheet" type="text/css" />
</head>
<div foo="<%= (string) ViewData[SomeNamespace.StyleSheetKey] %>">bar</div>

Now the div tag renders the stylesheet name properly, but the one in the link-tag is rendered as it is written, without being interpreted. In addition a path prefix is added.

So the ASP.NET engine seems to want to hassle with the text in the href- argument in the link tag, "helping" me to prefix my .css file with the correct relative path.

How will I now be able to set the name of the style sheet programmatically?

Upvotes: 1

Views: 1470

Answers (2)

David Glenn
David Glenn

Reputation: 24532

This will work

 <link href="<%= "" + ViewData[SomeNamespace.StyleSheetKey] %>" rel="stylesheet" type="text/css" />

but this doesn't

  <link href="<%= (string)ViewData[SomeNamespace.StyleSheetKey] %>" rel="stylesheet" type="text/css" />

Or as The_Butcher says, remove the runat="server" from the header

Upvotes: 1

The_Butcher
The_Butcher

Reputation: 2488

Ok, Try removing the runat="server" tag from the <head> tag

Upvotes: 1

Related Questions