简体   繁体   English

在ASP.NET Core Web App中,AddJsonOptions和AddJsonFormatters之间的区别是什么?

[英]In an ASP.NET Core Web App, what's the difference between AddJsonOptions and AddJsonFormatters?

I'm trying to control all json output settings across the board, like for normal HTTP 200 OK results to things like when model validation fails (HTTP 400 BAD Request) etc. 我试图控制所有 json输出设置,就像正常的HTTP 200 OK结果一样,当模型验证失败时(HTTP 400 BAD请求)等。

I ran across these two methods in startup.cs :- 我在startup.cs遇到了这两个方法: -

  • AddJsonOptions(options => ...)
  • AddJsonFormatters(options => ...)

Can someone please explain the difference between these two? 有人可以解释这两者之间的区别吗? Why I would use one over the other etc? 为什么我会使用一个而不是另一个?

FWIW, I'm also trying to use Newtonsoft JSON as my json provider under the hood, with settings like this: FWIW,我也试图使用Newtonsoft JSON作为我的json提供商,其设置如下:

var settings = new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver(),
    Formatting = Formatting.None,
    NullValueHandling = NullValueHandling.Ignore,
    DateFormatHandling = DateFormatHandling.IsoDateFormat
};

cheers! 干杯!

AddJsonOptions AddJsonOptions

This provides the means to configure the serailization settings and contract resolver which have already setup by calling .AddMvc() on the service collection. 这提供了通过在服务集合上调用.AddMvc()来配置已经设置的serailization设置和合同解析器的方法。 You can see from the source code that AddJsonFormatters() is already called by AddMvc() . 您可以从源代码中看到, AddJsonFormatters()已经调用了AddMvc() Hence, JSON.net will be used for serialization with a DefaultContractResolver and default settings provided. 因此,JSON.net将用于使用DefaultContractResolver进行序列化, 提供默认设置 However, you can use AddJsonOptions() in this situation to override the defaults and specify your desired contract resolver and serializer settings eg 但是,您可以在这种情况下使用AddJsonOptions()来覆盖默认值并指定所需的合约解析程序和序列化程序设置,例如

services.AddMvc()
    .AddJsonOptions(o => 
    {
        o.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        o.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
    });

AddJsonFormatters AddJsonFormatters

If you are using the barebones AddMvcCore() on your service collection, then formatting for JSON serialization middleware will not be setup by default ( see the source on GitHub repo ). 如果您在服务集合上使用准系统AddMvcCore() ,则默认情况下不会设置JSON序列化中间件的格式化( 请参阅GitHub repo上的源代码 )。 Consequently, you will need to call AddJsonFormatters() in order to explicitly setup and configure the resolver and serializer settings: 因此,您需要调用AddJsonFormatters()以显式设置和配置解析程序和序列化程序设置:

services.AddMvcCore()
    .AddJsonFormatters(o =>
    {
        o.ContractResolver = new CamelCasePropertyNamesContractResolver();
        o.NullValueHandling = NullValueHandling.Ignore;
    });

Summary 摘要

You can see that both of these methods are very similar. 您可以看到这两种方法非常相似。 The only reason that they both exist is because AddMvcCore() allows you to setup or create your own Serialization middleware. 它们都存在的唯一原因是因为AddMvcCore()允许您设置或创建自己的序列化中间件。 If you like the barebones setup that AddMvcCore() provides but want to use the JSON serialization formatting provided by AddMvc() then just call services.AddMvcCore().AddJsonFormatters() . 如果你喜欢准系统设置是AddMvcCore()提供,但是要使用提供的JSON序列化格式AddMvc()则只是调用services.AddMvcCore().AddJsonFormatters()

For a more in-depth description of the differences between AddMvc() and AddMvcCore() see the excellent post on What is the difference between AddMvc() and AddMvcCore()? 有关AddMvc()AddMvcCore()之间差异的更深入描述,请参阅优秀文章, 了解AddMvc()和AddMvcCore()之间的区别是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net Core 2.2 中 MvcJsonOptions 的 AddJsonOptions - AddJsonOptions for MvcJsonOptions in Asp.Net Core 2.2 在ASP.NET Core 2.0中找不到AddJsonOptions - AddJsonOptions not found in ASP.NET Core 2.0 Asp.Net Core中的IRequestCultureFeature和CurrentCulture有什么区别? - What's the difference between IRequestCultureFeature and CurrentCulture in Asp.Net Core? <a href="linkhere">ASP.NET Core</a><a asp-page="linkhere">和</a>in 有什么区别? - What's the difference between <a asp-page="linkhere"> and <a href="linkhere"> in ASP.NET Core? asp.net 内核中的控制台应用程序和 Web 应用程序之间的区别 - Difference between a console application and Web application in asp.net core WCF Web API和ASP.NET Web API之间有什么区别 - What's the difference between WCF Web API and ASP.NET Web API ASP.NET CORE中Projection和Join表之间有什么区别 - What's the difference between Projection and Join table in ASP.NET CORE Microsoft 标识平台和 ASP.NET 核心标识有什么区别? - What's the difference between the Microsoft identity platform and ASP.NET Core Identity? ASP.NET Core 中的 HttpRequest.Path 和 HttpRequest.PathBase 有什么区别? - What's the difference between HttpRequest.Path and HttpRequest.PathBase in ASP.NET Core? ASP.NET 核心托管和服务器端 Blazor 有什么区别,真的吗? - What's the difference between ASP.NET Core Hosted and Server-Side Blazor, really?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM