如何在Razor中转义html标记 [英] How to escape an html tag in Razor

查看:132
本文介绍了如何在Razor中转义html标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC中的View中,我需要键入以下文本行:

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

< h t m l>这就是您如何在Razor</h t m l>"中转义HTML标记的方法

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

请从<中删除空格. h t m l>认为它是正确的html标签.

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

我该如何完成?欢迎所有答案.

How do I accomplish it? All answers are welcome.

推荐答案

尝试一下.

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

打印

另一种简单的方法是:

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

提供与上述相同的输出.

gives same output as above.

这篇关于如何在Razor中转义html标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆