ASP.NET MVC Html.Encode - 新行

发布于 2024-07-13 06:51:56 字数 557 浏览 9 评论 0原文

Html.Encode 似乎只是简单地调用 HttpUtility.HtmlEncode 来用转义序列替换一些 html 特定字符。

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

我认为有两种选择,但也许有人可以建议更好的第三种选择。

一种选择是只编写一个使用 HtmlEncode 的静态方法,然后用
替换结果字符串中的新行,并用   替换多个空格组。 code>

另一种选择是在我的样式表中使用 white-space: pre 属性 - 但是我不确定当 Html 辅助方法包含新行和制表符时这是否会产生副作用使页面源漂亮。

是否有第三个选项,例如全局标志、事件或方法覆盖,我可以使用它来更改 html 编码的完成方式,而无需重做 html 帮助器方法?

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  

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半衾梦 2024-07-20 06:51:56

HtmlEncode 仅用于对字符进行编码以在 HTML 中显示。 它特别不编码空白字符。

我会选择你的第一个选项,并将其作为 HtmlHelper 的扩展方法。 类似于:

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

您可以使用 String.Replace() 来编码换行符和空格(如果需要更好的匹配,则可以使用 Regex.Replace )。

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

白首有我共你 2024-07-20 06:51:56

使用 style="white-space:pre-wrap;" 对我有用。 根据这篇文章

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

羁客 2024-07-20 06:51:56

如果您使用 Razor,您可以执行以下操作:

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

在您的视图中,或在您的控制器中:

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

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

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.

旧人哭 2024-07-20 06:51:56

将输出放入

和/或 块中。 例如:

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

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

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

取决于您是否想要语义标记还是想要摆弄 css。 我认为这些都比插入
>
标签更简洁。

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.

南风几经秋 2024-07-20 06:51:56
    /// <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 />"));
    }
    /// <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 />"));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文