ASP.NET MVC Html.Encode - New lines

2019-02-02 22:17发布

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

However this doesn't provide any consideration for how new lines and multiple spaces will be interpretted (markup whitespace). So I provide a text area for the a user to enter a plain text block of information, and then later display that data on another screen (using Html.Encode), the new lines and spacing will not be preserved.

I think there are 2 options, but maybe there is a better 3rd someone can suggest.

One option would be to just write a static method that uses HtmlEncode, and then replaces new lines in the resulting string with <br> and groups of multiple spaces with &nbsp;

Another option would be to mess about with the white-space: pre attribute in my style sheets - however I'm not sure if this would produce side effects when Html helper methods include new lines and tabbing to make the page source pretty.

Is there a third option, like a global flag, event or method override I can use to change how html encoding is done without having to redo the html helper methods?

5条回答
戒情不戒烟
2楼-- · 2019-02-02 22:42

HtmlEncode is only meant to encode characters for display in HTML. It specifically does not encode whitespace characters.

I would go with your first option, and make it an extension method for HtmlHelper. Something like:

public static string HtmlEncode(this HtmlHelper htmlHelper,
                                string text, 
                                bool preserveWhitespace)
{
    // ...
}

You could use String.Replace() to encode the newlines and spaces (or Regex.Replace if you need better matching).

查看更多
手持菜刀,她持情操
3楼-- · 2019-02-02 22:43

Using the style="white-space:pre-wrap;" worked for me. Per this article.

查看更多
何必那么认真
4楼-- · 2019-02-02 22:58

If you use Razor you can do:

@MvcHtmlString.Create(Html.Encode(strToEncode).Replace(Environment.NewLine, "<br />"))

in your view, or in your controller:

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

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

查看更多
可以哭但决不认输i
5楼-- · 2019-02-02 23:01

Put your output inside <pre></pre> and/or <code></code> blocks. E.g:

<pre>@someValue</pre> / <code>@someValue</code>

Use the equivalent css on an existing div:

<div style="white-space:pre-wrap;">@someValue</div>

Depends whether you want the semantic markup or whether you want to fiddle with css. I think these are both neater than inserting <br/> tags.

查看更多
欢心
6楼-- · 2019-02-02 23:06
    /// <summary>
    /// Returns html string with new lines as br tags
    /// </summary>
    public static MvcHtmlString ConvertNewLinesToBr<TModel>(this HtmlHelper<TModel> html, string text)
    {
        return  new MvcHtmlString(html.Encode(text).Replace(Environment.NewLine, "<br />"));
    }
查看更多
登录 后发表回答