ASP.NET MVC Html.Encode - New lines

后端 未结 5 1162
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-30 00:03

Html.Encode seems to simply call HttpUtility.HtmlEncode to replace a few html specific characters with their escape sequences.

However this

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 00:48

    If you use Razor you can do:

    @MvcHtmlString.Create(Html.Encode(strToEncode).Replace(Environment.NewLine, "
    "))

    in your view, or in your controller:

    HttpServerUtility httpUtil = new HttpServerUtility();
    MvcHtmlString encoded = httpUtil.HtmlEncode(strToEncode).Replace(Environment.NewLine, "
    ");

    I have not tested the controller method, but it should work the same way.

提交回复
热议问题