[英].NET Core 6- HtmlEncode response string through global declaration
While returning Json
Response from .NET Core 6 Web APIs, I want to encode all strings with WebUtility.HtmlEncode
.从 .NET Core 6 Web API 返回
Json
Response 时,我想使用WebUtility.HtmlEncode
对所有字符串进行编码。 As of now, I am writing this line for each string and I just came across this link: https://stackoverflow.com/a/44117929/3234665 .截至目前,我正在为每个字符串编写这一行,我刚刚看到这个链接: https://stackoverflow.com/a/44117929/3234665 。
Is there anyway we can declare this at some central/ global level (ie Attribute, Program.cs etc?) to reduce repetitive lines everywhere?无论如何,我们是否可以在某些中央/全局级别(即属性、Program.cs 等?)声明它以减少各处的重复行?
For globally add the custom ContractResolver
,you need add the configuration in your Program.cs like below:要全局添加自定义
ContractResolver
,您需要在 Program.cs 中添加配置,如下所示:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews().AddNewtonsoftJson();
//add this service to container...
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
ContractResolver = new CustomResolver(),
Formatting= Formatting.Indented,
DefaultValueHandling= DefaultValueHandling.Ignore, //add this
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.