用 <br/> 替换换行符在 ASP.NET MVC Razor 视图中 [英] Replace line break characters with <br /> in ASP.NET MVC Razor view

查看:26
本文介绍了用 <br/> 替换换行符在 ASP.NET MVC Razor 视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受输入的 textarea 控件.我稍后会尝试通过简单地使用:

I have a textarea control that accepts input. I am trying to later render that text to a view by simply using:

@Model.CommentText

@Model.CommentText

这是正确编码任何值.但是,我想用 <br/> 替换换行符,但我找不到确保新 br 标签不被编码的方法.我曾尝试使用 HtmlString 但还没有任何运气.

This is properly encoding any values. However, I want to replace the line break characters with <br /> and I can't find a way to make sure that the new br tags don't get encoded. I have tried using HtmlString but haven't had any luck yet.

推荐答案

使用 CSS 空白属性 而不是让自己暴露在 XSS 漏洞面前!

Use the CSS white-space property instead of opening yourself up to XSS vulnerabilities!

<span style="white-space: pre-line">@Model.CommentText</span>

这篇关于用 &lt;br/&gt; 替换换行符在 ASP.NET MVC Razor 视图中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆