How to escape an html tag in Razor

别来无恙 提交于 2020-07-20 20:24:39

问题


In a View in ASP.NET MVC, I need to type following line of text:

"< h t m l > This is how you escape an HTML tag in Razor < / h t m l >"

Please remove spaces from < h t m l > to consider it a proper html tag.

How do I accomplish it? All answers are welcome.


回答1:


Try this.

  @{ var myHtmlString = new HtmlString("<html> This is how you escape an HTML tag in Razor </html>");}
  @Html.Encode(myHtmlString)

Prints

Another Simple Way is :

  @Html.Encode(Html.Raw("<html> This is how you escape an HTML tag in Razor </html>"));

gives same output as above.



来源:https://stackoverflow.com/questions/45089555/how-to-escape-an-html-tag-in-razor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!