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
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)
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)