In .NET framework HTMLEncode
would encode special characters (such as Hungarian, Chinese letters) in view files and display them correctly, while in .NET 6 HTMLEncode
displays them as for example á
I am using UTF-8
encoding and all files are correctly set to it. I checked Microsoft documentation and I see these classes are from different Assembly
. Am I missing something here?
My goal is to display without special characters. I am aware of HtmlRaw
, but is this safe and the best way to go?
CodePudding user response:
By default, non-Latin characters are HTML-encoded by Razor in ASP.NET Core. When you use HTMLEncode
on them yourself, you are basically double-encoding them so that they render as their encoded version.
In short, there is no need to HTMLEncode
non-Latin characters in ASP.NET Core because the framework does it for you. You might, however, decide that you don't like the HTML equivalent of these characters in your HTML source code, in which case you can configure the encoder options to prevent the default encoders doing what they do and let the browser's UTF-8 support take care of things:
builder.Services.Configure<WebEncoderOptions>(options =>
{
options.TextEncoderSettings = new TextEncoderSettings(UnicodeRanges.BasicLatin, UnicodeRanges.Latin1Supplement);
});
Note that whatever you set here will override the default settings, which is why you need to include the BasicLatin
range. If you are unsure which character sets you should include, you can check here: http://www.unicode.org/charts/. Alternatively, you can simply specify UnicodeRanges.All
.