显示换行符 asp.net mvc razor
我使用以下命令使文本输出在
MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))
有更好的方法来做到这一点吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有一个更好/很棒的解决方案,它采用 CSS 空白属性:
使用此您可以避免 跨站脚本 (XSS) 漏洞...
与 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...
Works like a charm with ASP.NET MVC Razor engine.
您的代码很容易受到 XSS 攻击,因为它不对文本进行 HTML 编码。我会向您推荐以下内容:
然后您认为您可以安全地:
Your code is vulnerable to XSS attacks as it doesn't HTML encode the text. I would recommend you the following:
and then in your view you can safely:
也许您可以在
Maybe you can output the text inside a <pre> tag.
只需使用一个标签即可。
或者
Just use a tag.
<pre>@Model.Post.Description</pre>
Or
它对我有用。
字符串 Model.Text 具有 < br>>里面有标签。
It's working for me.
string Model.Text having < br/> tag inside.
这是我的解决方案。
当然,您必须添加以下 using 语句才能使正则表达式正常工作。
希望它对某人有用。
Here is my solution.
and of course, you will have to add following using statement for Regex to work.
Hope it is useful for someone.
只需执行以下命令,因为它已启用过滤:
Just do the following command as it has filtering enabled:
<p style="white-space: pre-line" <p>