简体   繁体   English

设置Asp.Net核心MVC Json选项

[英]Set Asp.Net Core MVC Json options

One of the classes that I have in my project called, say, AC , has a property Address that is of type IPEndPoint . 我在项目中的一个类叫做AC ,它有一个IPEndPoint类型的属性Address This type, as well as IPAddress , are notorious for not being serializable to JSON by default. 此类型以及IPAddress因默认情况下无法可序列化为JSON而臭名昭着。 Since I needed to serialize my class, I implemented two custom converters: IPAddressConverter and IPEndPointConverter . 由于我需要序列化我的类,我实现了两个自定义转换器: IPAddressConverterIPEndPointConverter To make Newtonsoft use these two converters, I made this class: 为了让Newtonsoft使用这两个转换器,我创建了这个类:

public sealed class CustomSettings : JsonSerializerSettings
{
    public CustomSettings() : base()
    {
        this.Converters.Add(new IPAddressConverter());
        this.Converters.Add(new IPEndPointConverter());
        this.TypeNameHandling = TypeNameHandling.Auto;
    }
} 

..which I use in my Main like so: ..我在我的Main用途如此:

Newtonsoft.Json.JsonConvert.DefaultSettings = () => new CustomSettings();

Now I am trying to add an API to my program. 现在我正在尝试为我的程序添加API。 I have created a .Net Core Web API project and have integrated it into my program successfully. 我创建了一个.Net Core Web API项目,并将其成功集成到我的程序中。 However, problems arose when I attempted to write a POST method that required an instance of AC in JSON form from the request body. 但是,当我尝试从请求主体编写一个需要JSON格式的AC实例的POST方法时,出现了问题。 The serializer couldn't convert the IPEndPoint , and thus the value of AC was always null . 序列化程序无法转换IPEndPoint ,因此AC的值始终为null

Information regarding configuration of .Net Core APIs is pretty sparse. 有关.Net Core API配置的信息非常少。 Can anyone tell me how I can pass those same settings to the MVC's serializer? 谁能告诉我如何将这些相同的设置传递给MVC的序列化器?

EDIT 编辑

I found a way (sort of). 我找到了一种方式(某种程度)。 Turns out you can set the JSON options in the ConfigureServices method. 事实证明,您可以在ConfigureServices方法中设置JSON选项。

I attempted to modify the MVC's serializer's settings the same way I did for the rest of my program by doing this: 我尝试修改MVC的序列化程序的设置,就像我对程序的其余部分所做的那样:

services.AddMvc().AddJsonOptions(options => options.SerializerSettings = new CustomSettings());

However, this doesn't work as options.SerializerSettings is read only. 但是,这不适用于options.SerializerSettings是只读的。

I could pass the converters one by one, but I'd prefer if they were all managed from one place (the CustomSettings class). 可以逐个传递转换器,但我更喜欢它们是从一个地方( CustomSettings类)管理的。 Is this possible? 这可能吗?

Create an extension method that encapsulates what it is you want configured 创建一个扩展方法,封装您要配置的内容

public static void AddCustomSettings(this Newtonsoft.Json.JsonSerializerSettings settings) {
    settings.Converters.Add(new IPAddressConverter());
    settings.Converters.Add(new IPEndPointConverter());
    settings.TypeNameHandling = TypeNameHandling.Auto;
}

And configure it in ConfigureServices 并在ConfigureServices配置它

services.AddJsonOptions(options => options.SerializerSettings.AddCustomSettings());

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM