[英]Setting JsonConvert.DefaultSettings asp net core 2.0 not working as expected
I have following code inside Startup.cs and expecting it to override default serialization options. 我在Startup.cs中有以下代码,并期望它覆盖默认的序列化选项。 I want it to override every single serialization throughout my asp net core 2.0 project, but action return value that is not correct, I think this global property is not working in core 2.0
我希望它覆盖我的asp net core 2.0项目中的每一个序列化,但是动作返回值不正确,我认为这个全局属性在core 2.0中不起作用
I have it written inside Configure exactly before app.UseMvc(); 我把它写在app.UseMvc()之前的配置中;
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
Formatting = Formatting.Indented,
TypeNameHandling = TypeNameHandling.Objects,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new List<JsonConverter> { new StringEnumConverter() }
};
In ASP.NET Core, this is configured when wiring up the services on the application in Startup.ConfigureServices
. 在ASP.NET Core中,这是在
Startup.ConfigureServices
应用程序上的服务时配置的。 There is an fluent AddJsonOptions(Action<MvcJsonOptions>)
extension to the IMvcBuilder
returned by the AddMvc()
extension. 有一个流利
AddJsonOptions(Action<MvcJsonOptions>)
延伸到IMvcBuilder
由返回AddMvc()
扩展。 MvcJsonOptions
exposes a SerializerSettings
property which you can configure in your action code. MvcJsonOptions
公开了一个SerializerSettings
属性,您可以在操作代码中配置该属性。
So instead of configuring once before registering MVC, it's done as part of the MVC registration. 因此,它不是在注册MVC之前配置一次,而是作为MVC注册的一部分完成的。
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());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.