asp.net core处理中文的指南
参考资料:https://docs.microsoft.com/en-us/aspnet/core/security/cross-site-scripting
Customizing the Encoders
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.
The reasoning behind this is to protect against unknown or future browser bugs (previous browser bugs have tripped up parsing based on the processing of non-English characters). If your web site makes heavy use of non-Latin characters, such as Chinese, Cyrillic or others this is probably not the behavior you want.
You can customize the encoder safe lists to include Unicode ranges appropriate to your application during startup, in ConfigureServices().
For example, using the default configuration you might use a Razor HtmlHelper like so;
<p>This link text is in Chinese: @Html.ActionLink("汉语/漢語", "Index")</p>
When you view the source of the web page you will see it has been rendered as follows, with the Chinese text encoded;
<p>This link text is in Chinese: <a href="/">汉语/漢語</a></p>
To widen the characters treated as safe by the encoder you would insert the following line into the ConfigureServices()method in startup.cs;
services.AddSingleton<HtmlEncoder>(
HtmlEncoder.Create(allowedRanges: new[] { UnicodeRanges.BasicLatin,
UnicodeRanges.CjkUnifiedIdeographs }));
This example widens the safe list to include the Unicode Range CjkUnifiedIdeographs. The rendered output would now become
<p>This link text is in Chinese: <a href="/">汉语/漢語</a></p>
Safe list ranges are specified as Unicode code charts, not languages. The Unicode standard has a list of code charts you can use to find the chart containing your characters. Each encoder, Html, JavaScript and Url, must be configured separately.
Note
Customization of the safe list only affects encoders sourced via DI. If you directly access an encoder via System.Text.Encodings.Web.*Encoder.Default then the default, Basic Latin only safelist will be used.
asp.net core处理中文的指南的更多相关文章
- C# -- HttpWebRequest 和 HttpWebResponse 的使用 C#编写扫雷游戏 使用IIS调试ASP.NET网站程序 WCF入门教程 ASP.Net Core开发(踩坑)指南 ASP.Net Core Razor+AdminLTE 小试牛刀 webservice创建、部署和调用 .net接收post请求并把数据转为字典格式
C# -- HttpWebRequest 和 HttpWebResponse 的使用 C# -- HttpWebRequest 和 HttpWebResponse 的使用 结合使用HttpWebReq ...
- ASP.NET Core Razor 页面使用指南
ASP.NET Core Razor 页面作为 ASP.NET Core 2.0的一部分发布,它是基于页面的全新的Web开发框架.如果您想学习如何使用 ASP.NET Core Razor 页面,可以 ...
- asp.net core输出中文乱码的问题
摘要 在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题. 问题重现 新建控制台和站点 public class Program { public static ...
- .NET CORE 2.0小白笔记(四):asp.net core输出中文乱码的问题
问题描述:在学习asp.net core的时候,尝试在控制台,或者页面上输出中文,会出现乱码的问题. 分析解决:控制台乱码的原因是因为中文windows命令行默认编码页是gb2312,想输出中文只要把 ...
- ASP.Net Core开发(踩坑)指南
ASP.NET与ASP.NET Core很类似,但它们之间存在一些细微区别以及ASP.NET Core中新增特性的使用方法,在此之前也写过一篇简单的对比文章ASP.NET MVC应用迁移到ASP.NE ...
- ASP.NET Core免费(视频)教程汇总
最近才开始学习ASP.NET Core,发现社区的学习资料很多,但是相关的视频教程不是很多,52ABP官方有两个视频教程,但是ABP框架比较臃肿,初学者学起来有点吃力,所以还是推荐大家先啃书或者官方文 ...
- ASP.NET Core的Kestrel服务器(转载)
Kestrel是一个基于libuv的跨平台ASP.NET Core web服务器,libuv是一个跨平台的异步I/O库.ASP.NET Core模板项目使用Kestrel作为默认的web服务器.Kes ...
- 百度地图和高德地图坐标系的互相转换 四种Sandcastle方法生成c#.net帮助类帮助文档 文档API生成神器SandCastle使用心得 ASP.NET Core
百度地图和高德地图坐标系的互相转换 GPS.谷歌.百度.高德坐标相互转换 一.在进行地图开发过程中,我们一般能接触到以下三种类型的地图坐标系: 1.WGS-84原始坐标系,一般用国际GPS纪录仪记 ...
- 新的ASP.NET Core 迁移指南
最近在微信里做了一个调查: Web Forms应用程序升级到.NET 6, 收到550份调查,调查还在继续,欢迎参与调查.可以访问链接:https://wj.qq.com/s2/9822949/ac3 ...
随机推荐
- io系列之其他事项
二.对IO异常的处理. io操作中,只要涉及到底层操作的就必须进行 io异常处理. IOException 是IO操作中必须处理的异常. 示例: class IOExceptionTest { pub ...
- Educational Codeforces Round 53 (Rated for Div. 2) A Diverse Substring
传送门 https://www.cnblogs.com/violet-acmer/p/10163375.html 题意: 给出串是多态的定义“长度为 n 的串 s ,当串 s 中所有字母出现的次数严格 ...
- maomao的fft板子
\(QwQ\) #include <cmath> #include <cstdio> #include <cstring> #include <iostrea ...
- (二叉树 bfs) leetcode 199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- python模块之时间模块
一.time模块 表示时间的方式分为: 1时间戳(timestamp) 2格式化化的时间字符串(format string) 3结构化时间(struct_time) import time print ...
- css3 rotate(1turn)的用法
1turn:一圈,一个圆共一圈. 90deg = 0.25turn.
- Linux记录-配置无密码登录
1.互信的机器都执行 ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub >> /home/hdfs/.ssh/authorized_keys chmod 60 ...
- 关于Android Studio开发环境变量的设置(avd启动黑屏)
之前因为乱按网上的设置导致启动avd启动黑屏,查了很久原来是ANDROID_AVD_HOME变量没有加$符号 以下是正确的环境变量配置 添加环境变量(注意avd中有一个$符号) ANDROID_SDK ...
- 2016vijos 1-2 股神小L(堆)
维护前i天的最优解,那么在后面可能会对前面几天的买卖情况进行调整 如果前面买入,买入的这个在后面一定不会卖出 如果前面卖出,卖出的这个可能会在后面变成买入,因为买这个,卖后面的会获得更多的收益 用一个 ...
- freemarker迭代list、map等常规操作,将数据放到模板中
转自:https://blog.csdn.net/wickedvalley/article/details/65937189 一.controller开始准备模型.数据1.po类 package co ...