ASP。净MVC Html。编码——新行

时间:2022-10-28 16:39:21

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

Html。编码似乎简单地称为HttpUtility。HtmlEncode将一些特定于html的字符替换为转义序列。

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.

但是,这并没有考虑如何解释新行和多个空格(标记空白)。因此,我为用户提供一个文本区域,让用户输入一个纯文本信息块,然后在另一个屏幕上显示该数据(使用Html.Encode),新行和空格将不会被保留。

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;

一种选择是编写一个使用HtmlEncode的静态方法,然后用
替换结果字符串中的新行,并用&

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.

另一种选择是在我的样式表中处理空白:pre属性——不过我不确定这是否会产生副作用,因为Html助手方法包括新行和tabbing,以使页面源变得漂亮。

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?

是否有第三个选项,比如全局标记、事件或方法覆盖,我可以使用它来改变html编码的方式,而不必重做html助手方法?

5 个解决方案

#1


23  

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

HtmlEncode只用于编码HTML中显示的字符。它特别不编码空白字符。

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

我将使用您的第一个选项,并使它成为HtmlHelper的扩展方法。喜欢的东西:

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).

可以使用String.Replace()对新行和空格(或Regex)进行编码。如果您需要更好的匹配,请进行替换)。

#2


7  

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

使用style="空白:预包装;"这篇文章。

#3


5  

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.

我还没有测试控制器方法,但它应该以相同的方式工作。

#4


2  

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

将输出放在

和/或 
 block >中。例句: 

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

Use the equivalent css on an existing div:

在现有的div中使用等效的css:

<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.

这取决于您是想要语义标记还是想要修改css。我认为这两个都比插入
标签更整洁。

#5


0  

    /// <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 />"));
    }

#1


23  

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

HtmlEncode只用于编码HTML中显示的字符。它特别不编码空白字符。

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

我将使用您的第一个选项,并使它成为HtmlHelper的扩展方法。喜欢的东西:

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).

可以使用String.Replace()对新行和空格(或Regex)进行编码。如果您需要更好的匹配,请进行替换)。

#2


7  

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

使用style="空白:预包装;"这篇文章。

#3


5  

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.

我还没有测试控制器方法,但它应该以相同的方式工作。

#4


2  

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

将输出放在

和/或 
 block >中。例句: 

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

Use the equivalent css on an existing div:

在现有的div中使用等效的css:

<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.

这取决于您是想要语义标记还是想要修改css。我认为这两个都比插入
标签更整洁。

#5


0  

    /// <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 />"));
    }