2010-09-08 52 views
12

@在asp.net中mvc 3 preview 1自动编码html,是否有另一种方法让html存在?在ASP.NET MVC 3中解码HTML

觉得这个场景:

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

这将刚刚打印出:<h1>stackoverflow</h1>

+0

什么将这个“计算器”参数是什么?一个div? – 2011-01-25 20:01:34

+0

@布鲁诺,只是文字。 – 2011-01-25 22:08:03

回答

19

您可以使用此

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

这将输出的HTML字符串没有编码

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

而且随着埃里克·波特的评论

+12

从MVC 3开始,您不再需要使用MvcHtmlString。 @(新的HtmlString(“

StackOverflow

”))将工作得很好。 IHtmlString的任何实现都可以工作。我们正在考虑一个帮助程序或快捷方式,以便将来为您创建新的HtmlString。 – 2010-09-08 17:04:22

14

来得有点晚,但现在有一个在MVC3方便的扩展方法: Html.Raw():

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

有没有什么办法让这个在声明助手(即 - 在App_Code中)工作?看起来像所有的Html助手都在那里被打破。 – 2011-05-09 02:17:20