Special characters in html output

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

If the output goes through Html.Encode() it won’t render the correct character, but ø

in the config I have

<globalization
  fileEncoding="utf-8"
  requestEncoding="utf-8"
  responseEncoding="utf-8"
  culture="nb-NO"
  uiCulture="nb-NO"
/>

Source:

<li><%: Html.ActionLink("Støvletter", "ListProducts", "Tag", new { tag = "Stovletter" }, null)%></li>

This outputs:

<li><a href="/Tag/Stovletter" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">Støvletter</a></li>

But output should look like:

<li><a href="/Tag/Stovletter" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">Støvletter</a></li>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

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

Method 3

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" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">Støvletter</a></li>

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

enter image description here


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x