MVC 3 escaping and unescaping the @
I am trying to make dynamic CSS import:
@foreach (string cssUrl in Model.Css)
{
@@import url(@Url.Content(cssUrl));
}
However this does not work, the @import is escaped but @Url... should not be! Wh开发者_Python百科at now?
You could place plain text in a <text>
tag:
@foreach (string cssUrl in Model.Css)
{
<text>@@import url(</text> @Url.Content(cssUrl)<text>);</text>
}
And this might work as well (haven't tried though):
@foreach (string cssUrl in Model.Css)
{
@@import url(@:@Url.Content(cssUrl));
}
Note that the <text></text>
tag will not be rendered. Here's a Razor Syntax Quick Reference by Phil Haack
Why don't you just render link tags for each url like this:
@foreach (string url in Model.Css)
{
<link rel="Stylesheet" type="text/css" href="@Url.Content(url)" />
}
精彩评论