在asp.net core 2.2中,我曾经有如下代码:
var jsonSettings = new JsonSerializerSettings
{
ContractResolver = new SubstituteNullWithEmptyStringContractResolver()
};
services.AddMvc(options =>
{
options.OutputFormatters.RemoveType<JsonOutputFormatter>();
options.OutputFormatters.Add(new ResponseJsonOutputFormatter(jsonSettings,ArrayPool<char>.Shared));
}
public class ResponseJsonOutputFormatter : JsonOutputFormatter
{
// Stuff in here
}
然而在3.0中使用:
services.AddControllersWithViews(options =>
类型JsonOutputFormatter
不再可用。
当前建议的全局自定义JSON响应的方式是什么?
我尝试使用IOutputFormatter
,但当我将它设置为AddControllersWithViews
中的OutputFormatters时,它似乎没有被连接,所以不确定是否需要额外的步骤?
使用新的端点路由中间件是一个选项吗?还是有更好的方法来实现这一点?
NewtonsoftJsonOutputFormatter
。 - tchelidze