【发布时间】:2021-05-04 01:49:02
【问题描述】:
我有 NET5 应用程序,在启动时我已将应用程序配置为使用 Newtonsoft 而不是 System.Text.Json. 对于发布 CSP 报告,我想添加 application/csp-report 作为支持的媒体类型。
即使我已经使用AddNewtonsoftJson 配置为使用Newtonsoft,NewtonsoftJsonInputFormatter 仍然不能用作输入格式化程序。以下代码返回 null
当尝试在 InputFormatters 集合中查找时。
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews(config =>
{
var jsonInputFormatter = options.InputFormatters
.OfType<NewtonsoftJsonInputFormatter>()
.First();
//jsonInputFormatter is null here
jsonInputFormatter.SupportedMediaTypes.Add("application/csp-report")
})
// Use Newtonsoft’s Json.NET instead of System.Text.Json.
.AddNewtonsoftJson((options)=>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
})
}
【问题讨论】:
标签: asp.net-core .net-core asp.net-core-mvc .net-5