However, if you're using Json.NET with 3.0+ (details in the docs), the SnakeCaseAttribute from above is still viable, with a couple of changes: JsonOutputFormatter现在是NewtonsoftJsonOutputFormatter. NewtonsoftJsonOutputFormatter构造函数需要参数MvcOptions.JsonOutputFormatter is now NewtonsoftJsonOutputFormatter.The NewtonsoftJsonOutputFormatter constructor requires an argument of MvcOptions.代码如下:public class SnakeCaseAttribute : ActionFilterAttribute{ public override void OnActionExecuted(ActionExecutedContext ctx) { if (ctx.Result is ObjectResult objectResult) { objectResult.Formatters.Add(new NewtonsoftJsonOutputFormatter( new JsonSerializerSettings { ContractResolver = new DefaultContractResolver { NamingStrategy = new SnakeCaseNamingStrategy() } }, ctx.HttpContext.RequestServices.GetRequiredService<ArrayPool<char>>(), ctx.HttpContext.RequestServices.GetRequiredService<IOptions<MvcOptions>>().Value)); } }}注意:例如,您可能想在某个地方提前创建JsonOutputFormatter/NewtonsoftJsonOutputFormatter -在本示例中,我没有走那么远,因为这是手头问题的次要问题.Note: You might want to create the JsonOutputFormatter/NewtonsoftJsonOutputFormatter ahead of time somewhere, for example - I've not gone that far in the example as that's secondary to the question at hand. 这篇关于更改单个ASP.NET Core控制器的JSON序列化设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!