【发布时间】:2020-03-23 17:18:23
【问题描述】:
在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 时,它似乎没有连接,所以不确定是否有额外的步骤?
可以选择带有新端点路由的中间件吗?还是有更好的方法来实现这一目标?
【问题讨论】:
-
Json.NET 的新兴替代品system.text.json,目前没有与 Json.NET 的合约解析器等效的公共。因此,您可能希望坚持使用 Json.NET。
-
替代方案是
NewtonsoftJsonOutputFormatter
标签: c# json asp.net-core-3.0