停止使用HTML Unicode转义的ASP.NET Core 2 MVC的剃刀引擎?
作者:互联网
通常,创建一个默认的ASP.NET Core 2 MVC项目,然后稍微修改一下操作:
public IActionResult About()
{
ViewData["Message"] = "This is Chinese[中文]";
return View();
}
这是视图(About.cshtml):
<h3>@ViewData["Message"]</h3>
<h3>这也是中文</h3>
这是浏览器中的输出结果:
<h3>This is Chinese[中文]</h3>
<h3>这也是中文</h3>
我发现以“ @”符号呈现的中文文本是html unicode转义的.问题是如何停止“ @”符号以使我的文字转义?
解决方法:
根据this article:
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>(
HtmlEncoder.Create(allowedRanges: new[] { UnicodeRanges.BasicLatin,
UnicodeRanges.CjkUnifiedIdeographs }));
这是此调整后的结果html代码:
<h3>This is Chinese[中文]</h3>
<h3>这也是中文</h3>
标签:asp-net-core,asp-net-core-mvc,asp-net-core-2-0,c 来源: https://codeday.me/bug/20191109/2012224.html