C# ASP.NET MVC Html.Encode - 新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/524528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC Html.Encode - New lines
提问by
Html.Encode
seems to simply call HttpUtility.HtmlEncode
to replace a few html specific characters with their escape sequences.
Html.Encode
似乎只是调用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
一种选择是只编写一个使用 HtmlEncode 的静态方法,然后将结果字符串中的新行替换<br>
为多个空格的组
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.
另一种选择是弄乱white-space: pre
样式表中的属性 - 但是我不确定当 Html 辅助方法包含新行和制表符以使页面源变得漂亮时,这是否会产生副作用。
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 帮助程序方法?
采纳答案by Brannon
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.Replace
如果您需要更好的匹配)。
回答by Tim Abell
Put your output inside <pre></pre>
and/or <code></code>
blocks. E.g:
将您的输出放在<pre></pre>
和/或<code></code>
块内。例如:
<pre>@someValue</pre> / <code>@someValue</code>
Use the equivalent css on an existing div
:
在现有的上使用等效的 css 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.
取决于您是想要语义标记还是想要摆弄 css。我认为这些都比插入<br/>
标签更整洁。
回答by Alex
Using the style="white-space:pre-wrap;"
worked for me. Per this article.
使用style="white-space:pre-wrap;"
对我有用的。根据这篇文章。
回答by TheUO
If you use Razor you can do:
如果您使用 Razor,您可以:
@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.
我没有测试控制器方法,但它应该以相同的方式工作。
回答by RitchieD
/// <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 />"));
}