asp.net-mvc asp.net mvc 将 \n 换行转换为 html 中断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5032097/
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 convert \n new line to html breaks
提问by Shawn Mclean
I have a textarea
in mvc. When data is entered into that and I'm displaying it back to the user, how do I show the line breaks?
我textarea
在 mvc 中有一个。当输入数据并将其显示给用户时,如何显示换行符?
I display like this:
我这样显示:
<%= Model.Description%>
回答by Codo
The following class implements a HtmlHelper that properly encodes the text:
以下类实现了一个正确编码文本的 HtmlHelper:
public static class HtmlExtensions
{
public static MvcHtmlString Nl2Br(this HtmlHelper htmlHelper, string text)
{
if (string.IsNullOrEmpty(text))
return MvcHtmlString.Create(text);
else
{
StringBuilder builder = new StringBuilder();
string[] lines = text.Split('\n');
for (int i = 0; i < lines.Length; i++)
{
if (i > 0)
builder.Append("<br/>\n");
builder.Append(HttpUtility.HtmlEncode(lines[i]));
}
return MvcHtmlString.Create(builder.ToString());
}
}
}
It's easy to use in your views:
在您的视图中很容易使用:
<%= Html.Nl2Br(Model.MultilineText) %>
Or with Razor:
或使用剃刀:
@Html.Nl2Br(Model.MultilineText)
回答by mkataja
For a slightly different (and, IMHO, better and safer) way of solving the problem, see this answer to a similar question.
对于稍微不同(并且,恕我直言,更好,更安全)的解决问题的方法,请参阅类似问题的答案。
Basically, instead of going through the trouble of converting all new line characters to <br>
elements, it is as simple as applying the following CSS to the element where you are showing the inputted text:
基本上,无需将所有换行符转换为<br>
元素,只需将以下 CSS 应用于显示输入文本的元素即可:
white-space: pre-line
回答by David Glenn
You should always encode user entered textwhen displaying it back in the view to ensure that it is safe.
在视图中显示回用户输入的文本时,您应该始终对其进行编码,以确保它是安全的。
You could do as Cybernate suggested or could add it to a HtmlHelper extension method
您可以按照 Cybernate 的建议进行操作,也可以将其添加到 HtmlHelper 扩展方法中
public static string EncodedMultiLineText(this HtmlHelper helper, string text) {
if (String.IsNullOrEmpty(text)) {
return String.Empty;
}
return Regex.Replace(helper.Encode(text), Environment.NewLine, "<br/>")
}
So that it can be easily reused in you view
以便它可以在您的视图中轻松重复使用
<%= Html.EncodedMultiLineText(Model.Description) %>
回答by Chandu
Try something like this:
尝试这样的事情:
<%=
Regex.Replace(
Html.Encode(Model.Description),
Environment.NewLine,
"<br/>",
RegexOptions.IgnoreCase||RegexOptions.Multiline
)
%>
回答by Roland Schaer
The answer above that provides a static HTML helper extension provides an obsolete constructor:
上面提供静态 HTML 帮助程序扩展的答案提供了一个过时的构造函数:
return new MvcHtmlString(builder.ToString());
This line can be replaced by the following line of code:
这行代码可以替换为以下代码行:
return MvcHtmlString.Create((builder.ToString()));
回答by Raúl Ota?o
I think this answersolves the problems in a nice way, just using css: style="white-space: pre-line"
check also: CSS white space property.
我认为这个答案以一种很好的方式解决了问题,只需使用 css: style="white-space: pre-line"
check also: CSS white space property。
回答by ?brahim ?zb?lük
For Asp.net mvc razor,i used Html.Encode for blocking xss attacks
对于 Asp.net mvc razor,我使用 Html.Encode 来阻止 xss 攻击
@Html.Raw(Html.Encode(Model.Description).Replace(Environment.NewLine, "<br />"))
回答by Alvis
I like using HtmlExtensions, Environment.NewLine and Regex, which were in different answers, so I kinda put the above answers into a single method.
我喜欢使用 HtmlExtensions、Environment.NewLine 和 Regex,它们在不同的答案中,所以我有点把上面的答案放在一个方法中。
public static MvcHtmlString MultiLineText(this HtmlHelper htmlHelper, string text) {
if (string.IsNullOrEmpty(text)) {
return MvcHtmlString.Create(string.Empty);
}
return MvcHtmlString.Create(Regex.Replace(HttpUtility.HtmlEncode(text), Environment.NewLine, "<br/>"));
}
Use
用
<%= Html.MultiLineText(Model.MultilineText) %>
Or
或者
@Html.MultiLineText(Model.MultilineText)
回答by Hemant Tank
This works for me -
这对我有用-
<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT <br/> Claim #", "actionName" ))%>
<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT <br/> Claim #", "actionName" ))%>
回答by alexl
Maybe it's the answer: How do you handle line breaks in HTML Encoded MVC view?
也许这就是答案:您如何处理 HTML 编码的 MVC 视图中的换行符?
or
或者
you can try:
你可以试试:
Model.Description.Replace(Environment.NewLine, "<br/>");