Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decoding HTML in ASP.NET MVC 3

@ in asp.net mvc 3 preview 1 automaticly encodes html, is there an alternative way to let there be html?

think of this scenario:

@view.BestSitesEver.Replace("stackoverflow", "<h1>StackOverflow</h1>")

That would just print out: <h1>stackoverflow</h1>

like image 289
Filip Ekberg Avatar asked Sep 08 '10 08:09

Filip Ekberg


2 Answers

You can use this

@MvcHtmlString.Create(site.Replace("stackoverflow", "<h1>stackoverflow</h1>"))

This will output the html string without encoding

@(new HtmlString(site.Replace("stackoverflow", "<h1>stackoverflow</h1>")))

And with Erik Porter's comment

like image 169
Buildstarted Avatar answered Oct 15 '22 04:10

Buildstarted


A little bit late now but there's a convenient extension method in MVC3: Html.Raw():

@Html.Raw(site.Replace("stackoverflow", "<h1>stackoverflow</h1>"))
like image 30
David Rettenbacher Avatar answered Oct 15 '22 04:10

David Rettenbacher