开发者

MVC 3 escaping and unescaping the @

开发者 https://www.devze.com 2023-03-01 00:09 出处:网络
I am trying to make dynamic CSS import: @foreach (string cssUrl in Model.Css) { @@import url(@Url.Content(cssUrl));

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)" />
}
0

精彩评论

暂无评论...
验证码 换一张
取 消