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 th开发者_运维问答e name of the style sheet programmatically?
Ok, Try removing the runat="server" tag from the <head>
tag
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
精彩评论