Special characters in html output

前端 未结 3 806
既然无缘
既然无缘 2021-01-03 10:52

I want characters like \'ø\' to be printed directly, but in the source \'ø\' shows up as ø

If the output goes through Html.Encode()

相关标签:
3条回答
  • 2021-01-03 10:57

    You could try to create output with <%= instead of html encoded <%:

    (I post this after your positive comment to my comment)

    0 讨论(0)
  • 2021-01-03 11:03

    AFAIK this is not possible unless you roll your own custom HTML encoding routine (which I absolutely would recommend you against).

    The resulting HTML is correct and the character is properly displayed on the client browser. HTML is meant to be read by programs (web browsers) in order to present it to a human readable form, not by humans.

    <li><a href="/Tag/Stovletter">St&#248;vletter</a></li>
    

    is a perfectly valid HTML that will be displayed like this on the client browser:

    enter image description here

    0 讨论(0)
  • 2021-01-03 11:04

    This might not be the answer you are looking for but it is nevertheless interesting.

    If you look at the HttpEncoder source code it makes provision for custom HtmlEncoders to be used instead of the default WebUtility.HtmlEncode

    I have played around and found the HtmlEncoder called AntiXSS from Microsoft encodes these characters correctly.

    I installed it using Nuget:

    PM> Install-Package AntiXSS 
    

    And then updated my web.config as such:

    <system.web>
        <httpRuntime encoderType="Microsoft.Security.Application.AntiXssEncoder, 
    AntiXssLibrary" />
        ....
    </system.web>
    

    Both normal output and Html.ActionLinks seem to work.

    0 讨论(0)
提交回复
热议问题