Writing/outputting HTML strings unescaped

asp.net Mvcasp.net Mvc-3Razor

asp.net Mvc Problem Overview


I've got safe/sanitized HTML saved in a DB table.

How can I have this HTML content written out in a Razor view?

It always escapes characters like < and ampersands to &amp;.

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

Supposing your content is inside a string named mystring...

You can use:

@Html.Raw(mystring)

Alternatively you can convert your string to HtmlString or any other type that implements IHtmlString in model or directly inline and use regular @:

@{ var myHtmlString = new HtmlString(mystring);}
@myHtmlString

Solution 2 - asp.net Mvc

In ASP.NET MVC 3 You should do something like this:

// Say you have a bit of HTML like this in your controller:
ViewBag.Stuff = "<li>Menu</li>"
//  Then you can do this in your view:
@MvcHtmlString.Create(ViewBag.Stuff)

Solution 3 - asp.net Mvc

You can use

@{ WriteLiteral("html string"); }

Solution 4 - asp.net Mvc

Sometimes it can be tricky to use raw html. Mostly because of XSS vulnerability. If that is a concern, but you still want to use raw html, you can encode the scary parts.

@Html.Raw("(<b>" + Html.Encode("<script>console.log('insert')</script>" + "Hello") + "</b>)")

Results in

(<b>&lt;script&gt;console.log('insert')&lt;/script&gt;Hello</b>)

Solution 5 - asp.net Mvc

You can put your string into viewdata in controller like this :

 ViewData["string"] = DBstring;

And then call that viewdata in view like this :

@Html.Raw(ViewData["string"].ToString())

Solution 6 - asp.net Mvc

Apart from using @MvcHtmlString.Create(ViewBag.Stuff) as suggested by Dommer, I suggest you to also use AntiXSS library as suggested phill http://haacked.com/archive/2010/04/06/using-antixss-as-the-default-encoder-for-asp-net.aspx

It encodes almost all the possible XSS attack string.

Solution 7 - asp.net Mvc

Complete example for using template functions in RazorEngine (for email generation, for example):

@model SomeModel
@{
    Func<PropertyChangeInfo, object> PropInfo =
        @<tr class="property">
            <td>
                @item.PropertyName                
            </td>
            <td class="value">
                <small class="old">@item.OldValue</small>
                <small class="new">@item.CurrentValue</small>                
            </td>
        </tr>;
}

<body>

@{ WriteLiteral(PropInfo(new PropertyChangeInfo("p1", @Model.Id, 2)).ToString()); }

</body>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAGSView Question on Stackoverflow
Solution 1 - asp.net MvcLorenzoView Answer on Stackoverflow
Solution 2 - asp.net MvcTom ChantlerView Answer on Stackoverflow
Solution 3 - asp.net MvcAndrusView Answer on Stackoverflow
Solution 4 - asp.net MvcTravis JView Answer on Stackoverflow
Solution 5 - asp.net MvcAjayView Answer on Stackoverflow
Solution 6 - asp.net MvcZeNoView Answer on Stackoverflow
Solution 7 - asp.net MvcZlobnyiSergView Answer on Stackoverflow