Hot to escape and unescape @ in mvc?

Total Post:134

Points:940
 1689  View(s)
Ratings:
Rate this:
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! What now?

  1. Post:28

    Points:196
    Re: Hot to escape and unescape @ in mvc?

    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.

      Modified On Apr-06-2018 01:43:05 AM

Answer