Disable encoding of unicode characters in ASP.NET-MVC3

后端 未结 2 1567
别跟我提以往
别跟我提以往 2021-01-19 15:37

On my site every text is served as UTF-8.

Since nowadays every browser supports unicode characters, I would like to use them as-is.

The asp.net framework is

相关标签:
2条回答
  • 2021-01-19 15:58

    I'm afraid that you cannot turn this encoding feature off. This "nice" feature is provided by the WebUtility.HtmlEncode and you cannot influence the encoding.

    However with starting .net 4.0 you can customize the encoding behavior, with creating a class that inherits from the HttpEncoder and configure it in the web.cofig HttpRuntimeSection.EncoderType. But you need to implement your own custom encoding logic.

    Luckily .net 4.5 ships with a new HttpEncoder which encodes the bad stuff (like <script>) however handles the Unicode characters correctly called AntiXssEncoder

    So you just need to add this in your web.config:

    <system.web>
        <httpRuntime encoderType="System.Web.Security.AntiXss.AntiXssEncoder, 
                                 System.Web, Version=4.0.0.0, Culture=neutral, 
                                 PublicKeyToken=b03f5f7f11d50a3a"/>    
    </system.web>
    

    If you are not yet on .net 4.5 you can implement your AntiXssEncoder with the help of Microsoft Web Protection Library

    Here is an article how to set it up: Using AntiXss As The Default Encoder For ASP.NET (although it might be outdated)

    0 讨论(0)
  • 2021-01-19 16:10

    You can also use the @Html.Raw method of mvc.This is useful where you don't want to do it at global level sometimes on already built project.

    @Html.Raw(@ViewBag.Title)
    
    0 讨论(0)
提交回复
热议问题