【问题标题】:How to escape an html tag in Razor如何在 Razor 中转义 html 标签
【发布时间】:2017-12-18 18:18:58
【问题描述】:

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

" 这是在 Razor 中转义 HTML 标记的方式 h t m l >"

请从 中删除空格以将其视为正确的 html 标记。

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

【问题讨论】:

    标签: c# html asp.net-mvc razor


    【解决方案1】:

    试试这个。

      @{ 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>"));
    

    提供与上述相同的输出。

    【讨论】:

    • 好的,这是正确的。感谢您的回答和帮助。嗯,你知道string x ="hi";var x = new HtmlString("hi");有什么区别吗。
    • HtmlStringstring 的原始格式。只有当字符串中有html tags 时,您才能看到差异。 @Html.Raw(string)返回@HtmlString(string).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    相关资源
    最近更新 更多