开发者

Decoding HTML in ASP.NET MVC 3

开发者 https://www.devze.com 2023-01-15 11:31 出处:网络
@ in asp.net mvc 3 preview 1 automaticly encodes html, is there an alternative way to let there be html?

@ 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 wo开发者_JAVA技巧uld just print out: <h1>stackoverflow</h1>


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


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

@Html.Raw(site.Replace("stackoverflow", "<h1>stackoverflow</h1>"))
0

精彩评论

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