Setting JsonConvert.DefaultSettings asp net core 2.0 not working as expected

后端 未结 1 1387
滥情空心
滥情空心 2020-12-10 17:28

I have following code inside Startup.cs and expecting it to override default serialization options. I want it to override every single serialization throughout my asp net co

相关标签:
1条回答
  • 2020-12-10 17:54

    In ASP.NET Core, this is configured when wiring up the services on the application in Startup.ConfigureServices. There is an fluent AddJsonOptions(Action<MvcJsonOptions>) extension to the IMvcBuilder returned by the AddMvc() extension. MvcJsonOptions exposes a SerializerSettings property which you can configure in your action code.

    So instead of configuring once before registering MVC, it's done as part of the MVC registration.

    Example incorporating your setup:

    services.AddMvc()
      .AddJsonOptions( options =>
      {
        options.SerializerSettings.Formatting = Formatting.Indented;
        options.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects;
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        options.SerializerSettings.Converters.Add(new StringEnumConverter());
      });
    
    0 讨论(0)
提交回复
热议问题