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 &
.
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 &
.
You can put your string into viewdata in controller like this :
And then call that viewdata in view like this :
You can use
Supposing your content is inside a string named
mystring
...You can use:
Alternatively you can convert your string to
HtmlString
or any other type that implementsIHtmlString
in model or directly inline and use regular@
:In ASP.NET MVC 3 You should do something like this:
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.aspxIt encodes almost all the possible XSS attack string.
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.
Results in