Asked the classmate yesterday to ask me to raise such a question:. NET Core Chinese and other non-English text HTML encoding output problem. Small series yesterday himself also hands-on experiment, found that there is a problem, the following, on this issue, the small sum summed up some things!
For example, the following Razor View Code:
@{
Viewbag.title = "Code changes the World";
}
The HTML code for the output becomes:
The above @ViewBag. Title is actually equivalent to the following code:
@Html. Raw (Html.encode (Viewbag.title))
So solving this problem requires starting with HtmlHelper in ASP. NET Core MVC (the type of HTML in the code above is HtmlHelper).
Check out the MVC source code from GitHub to see the implementation of Htmlhelper.encode ():
Private ReadOnly Ihtmlgenerator _htmlgenerator;
public string Encode (string value)
{
return _htmlgenerator.encode (value);
}
The actual call is the Ihtmlgenerator interface of the Encode () method, MVC implementation of this interface is Defaulthtmlgenerator, the corresponding encode () implementation code is as follows:
Private ReadOnly Htmlencoder _htmlencoder;
public string Encode (string value)
{
Return!string. IsNullOrEmpty (value)? _htmlencoder.encode (value): String. Empty;
}
The main protagonist of the original work is Htmlencoder, but it is not implemented in MVC, but is implemented in the. NET Core framework, and the namespace is System.Text.Encodings.Web.
Write A. NET Core console program to call Htmlencoder to see if it's what's causing it.
public class Program
{
public static void Main (string[] args)
{
Console.WriteLine (HtmlEncoder.Default.Encode ("Code Changes the World"));
}
}
The output is the same problem as in MVC.
Try not to use the default Htmlencoder instance (Htmlencoder.default), but instead call the Htmlencoder.create () method to create the instance, and the Unicoderange parameter type is found.
public static Htmlencoder Create (params unicoderange[] allowedranges);
When you create an Htmlencoder instance using Unicoderanges.all as a parameter, the problem is resolved.
Console.WriteLine (Htmlencoder.create (Unicoderanges.all). Encode ("Code Changes the World"));
Then check out the source code for SYSTEM.TEXT.ENCODINGS.WEB from GitHub and see how the Htmlencoder.default corresponding HTMLEncode instance was created:
Internal readonly static Defaulthtmlencoder Singleton = new Defaulthtmlencoder (New Textencodersettings ( Unicoderanges.basiclatin));
Originally used is unicoderanges.basiclatin, no wonder Chinese will be encoded, do not understand why the default is not Unicoderanges.all?
Knowing the cause of the problem is easy to solve, as long as we create the Htmlencoder instance with Htmlencoder.create (Unicoderanges.all) and replace the default Htmlencoder instance used in MVC. How to replace it?
Back in the MVC source code, look at the implementation of Defaulthtmlgenerator, and find that it has htmlencoder in its constructor arguments:
Public Defaulthtmlgenerator (
Iantiforgery Antiforgery,
Ioptions Optionsaccessor,
Imodelmetadataprovider Metadataprovider,
Iurlhelperfactory Urlhelperfactory,
Htmlencoder Htmlencoder,
Clientvalidatorcache Clientvalidatorcache)
{
}
Under. NET from top to bottom, from the inside out full dependency injection of temperament, this place should also be dependent injection, we just need to inject a new Htmlencoder instance, is that it?
Code on a line, you'll know.
Add the following line of code in the Configureservices () method of Startup.cs:
Services. Addsingleton (Htmlencoder.create (Unicoderanges.all));
Run the ASP. NET core site with the following output:
A line of injections, immediately resolved. The power of dependency injection, the fascination of. NET Core.
. NET tutorial to solve the problem of HTML encoding in Chinese in MVC view