通常,创建一个默认的ASP.NET Core 2 MVC项目,然后稍微修改一下操作:
public IActionResult About()
{
ViewData["Message"] = "This is Chinese[中文]";
return View();
}
这是视图(About.cshtml):
@ViewData["Message"]
这也是中文
这是浏览器中的输出结果:
This is Chinese[中文]
这也是中文
我发现以“ @”符号呈现的中文文本是html unicode转义的.问题是如何停止“ @”符号以使我的文字转义?
解决方法:
By default encoders use a safe list limited to the Basic Latin Unicode
range and encode all characters outside of that range as their
character code equivalents. This behavior also affects Razor TagHelper
and HtmlHelper rendering as it will use the encoders to output your
strings.
本文还提供了自定义默认html编码器的正确方法.将以下注册添加到Startup.ConfigureServices方法:
services.AddSingleton(
HtmlEncoder.Create(allowedRanges: new[] { UnicodeRanges.BasicLatin,
UnicodeRanges.CjkUnifiedIdeographs }));
这是此调整后的结果html代码:
This is Chinese[中文]
这也是中文
标签:asp-net-core,asp-net-core-mvc,asp-net-core-2-0,c
来源: https://codeday.me/bug/20191109/2012224.html