HTML.En code,但preserve换行符 [英] HTML.Encode but preserve line breaks

查看:144
本文介绍了HTML.En code,但preserve换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采取用户输入到文本区,存储它,并最终显示它给用户。

I take user input into a text area, store it and eventually display it back to the user.

在我看来(剃刀)我想要做这样的事情...

In my View (Razor) I want to do something like this...

@Message.Replace("\n", "</br>")

这并不因为剃刀的Html恩codeS默认工作。这是伟大的,但我希望我的换行符。

This doesn't work because Razor Html Encodes by default. This is great but I want my line breaks.

如果我这样做,我得到开辟了XSS问题。

If I do this I get opened up to XSS problems.

@Html.Raw(Message.Replace("\n", "</br>"))

什么是处理这种情况的正确方法?

What's the right way to handle this situation?

推荐答案

使用HttpUtility.HtmlEn code然后进行替换。

Use HttpUtility.HtmlEncode then do the replace.

@Html.Raw(HttpUtility.HtmlEncode(Message).Replace("\n", "<br/>"))

这篇关于HTML.En code,但preserve换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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