显示换行符 asp.net mvc razor

发布于 2024-10-21 15:47:12 字数 223 浏览 10 评论 0 原文

我使用以下命令使文本输出在

MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))

有更好的方法来做到这一点吗?

I'm using the following to make the text output the line breaks entered in a <textarea> HTML element.

MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))

Is there a nicer way to do this?

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

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

发布评论

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

评论(7

月朦胧 2024-10-28 15:47:12

有一个更好/很棒的解决方案,它采用 CSS 空白属性

使用此您可以避免 跨站脚本 (XSS) 漏洞...

<p style="white-space: pre-line">@Model.Message</p>

与 ASP 一起工作就像一个魅力.NET MVC Razor 引擎。

There's an even better/awesome solution that employs CSS white-space property:

Using this you avoid Cross-site scripting (XSS) vulnerabilities...

<p style="white-space: pre-line">@Model.Message</p>

Works like a charm with ASP.NET MVC Razor engine.

一江春梦 2024-10-28 15:47:12

您的代码很容易受到 XSS 攻击,因为它不对文本进行 HTML 编码。我会向您推荐以下内容:

var result = string.Join(
    "<br/>",
    Model.Post.Description
        .Split(new[] { Environment.NewLine }, StringSplitOptions.None)
        .Select(x => HttpUtility.HtmlEncode(x))
);
return MvcHtmlString.Create(result);

然后您认为您可以安全地:

@Html.SomeHelper()

Your code is vulnerable to XSS attacks as it doesn't HTML encode the text. I would recommend you the following:

var result = string.Join(
    "<br/>",
    Model.Post.Description
        .Split(new[] { Environment.NewLine }, StringSplitOptions.None)
        .Select(x => HttpUtility.HtmlEncode(x))
);
return MvcHtmlString.Create(result);

and then in your view you can safely:

@Html.SomeHelper()
云归处 2024-10-28 15:47:12

也许您可以在

 中输出文本。标签。

Maybe you can output the text inside a <pre> tag.

故事未完 2024-10-28 15:47:12

只需使用一个标签即可。

@Model.Post.Description

或者

@Html.Raw(HttpUtility.HtmlDecode(Model.Post.Description.Replace("\r\n", "<br>")))

Just use a tag.
<pre>@Model.Post.Description</pre>

Or

@Html.Raw(HttpUtility.HtmlDecode(Model.Post.Description.Replace("\r\n", "<br>")))
離人涙 2024-10-28 15:47:12

它对我有用。

<p class="message">
@Html.Raw("<p>" + Model.Text + "</p>")
</p>

字符串 Model.Text 具有 < br>>里面有标签。

It's working for me.

<p class="message">
@Html.Raw("<p>" + Model.Text + "</p>")
</p>

string Model.Text having < br/> tag inside.

彻夜缠绵 2024-10-28 15:47:12

这是我的解决方案。

@MvcHtmlString.Create(Regex.Replace(Html.Encode(Model.Address), Environment.NewLine, "<br />", RegexOptions.Multiline))

当然,您必须添加以下 using 语句才能使正则表达式正常工作。

@using System.Text.RegularExpressions

希望它对某人有用。

Here is my solution.

@MvcHtmlString.Create(Regex.Replace(Html.Encode(Model.Address), Environment.NewLine, "<br />", RegexOptions.Multiline))

and of course, you will have to add following using statement for Regex to work.

@using System.Text.RegularExpressions

Hope it is useful for someone.

夏花。依旧 2024-10-28 15:47:12

只需执行以下命令,因为它已启用过滤:

Just do the following command as it has filtering enabled:

<p style="white-space: pre-line" <p>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文