ASP.NET MVC Razor render without encoding

后端 未结 7 2251
悲&欢浪女
悲&欢浪女 2020-11-29 02:42

Razor encodes string by default. Is there any special syntax for rendering without encoding?

7条回答
  •  再見小時候
    2020-11-29 03:08

    Use @Html.Raw() with caution as you may cause more trouble with encoding and security. I understand the use case as I had to do this myself, but carefully... Just avoid allowing all text through. For example only preserve/convert specific character sequences and always encode the rest:

    @Html.Raw(Html.Encode(myString).Replace("\n", "
    "))

    Then you have peace of mind that you haven't created a potential security hole and any special/foreign characters are displayed correctly in all browsers.

提交回复
热议问题