Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JsonApiSerializer设置为默认的输入和输出序列化程序C#.NET Core 3+;_C#_.net_Web_Ember.js_Json Api - Fatal编程技术网

如何将JsonApiSerializer设置为默认的输入和输出序列化程序C#.NET Core 3+;

如何将JsonApiSerializer设置为默认的输入和输出序列化程序C#.NET Core 3+;,c#,.net,web,ember.js,json-api,C#,.net,Web,Ember.js,Json Api,我一直在尝试将JsonApiSerializer()设置为输入和输出的默认序列化程序,以便为带有Ember数据的Ember应用程序提供一个符合json:api的.NET服务器 通过将以下代码写入Startup.cs文件,我成功地设置了输出序列化程序: public void ConfigureServices(IServiceCollection services) { services.AddControllers(); serv

我一直在尝试将JsonApiSerializer()设置为输入和输出的默认序列化程序,以便为带有Ember数据的Ember应用程序提供一个符合json:api的.NET服务器

通过将以下代码写入Startup.cs文件,我成功地设置了输出序列化程序:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddDbContext<Context>(options => options.UseSqlServer(Configuration.GetConnectionString("myDb")));

            var sp = services.BuildServiceProvider();
            var logger = sp.GetService<ILoggerFactory>();
            var objectPoolProvider = sp.GetService<ObjectPoolProvider>();

            services.AddMvc(opt =>
            {
                var serializerSettings = new JsonApiSerializerSettings();
**
                var jsonApiFormatter = new NewtonsoftJsonOutputFormatter(serializerSettings, ArrayPool<Char>.Shared, opt);
                opt.OutputFormatters.RemoveType<NewtonsoftJsonOutputFormatter>();
                opt.OutputFormatters.Insert(0, jsonApiFormatter);
**
                var mvcNewtonsoftSettings = new MvcNewtonsoftJsonOptions();

                var jsonApiInputFormatter = new NewtonsoftJsonInputFormatter(logger.CreateLogger<NewtonsoftJsonInputFormatter>(), serializerSettings, ArrayPool<Char>.Shared, objectPoolProvider, opt, mvcNewtonsoftSettings);
                opt.InputFormatters.RemoveType<NewtonsoftJsonInputFormatter>();
                opt.InputFormatters.Insert(0, jsonApiInputFormatter);
                //opt.InputFormatters.OfType<NewtonsoftJsonInputFormatter>().FirstOrDefault().SupportedMediaTypes.Add("application/vnd.api+json");

            }).AddNewtonsoftJson();
        }
用于发布制造商的.NET服务器的绑定模型如下所示:

public class CreateManufacturerBindingModel
    {
        public string Type { get; set; } = "manufacturer";
        public string Name { get; set; }
        public AssignProductBidingModel[] Products { get; set; }
    }

public class AssignProductBidingModel
    {
        public string Type { get; set; } = "product";
        public int Id { get; set; }
    }
我已经尝试了这些代码的一些变体有一段时间了,我能得到的只是CreateManufacturerBindingModel的“Name”“Products”属性的Null

有人知道我为什么一直在.NET服务器上获取空值吗


谢谢你的关注

ASP.NET Core 3.1默认情况下将使用System.Text.Json序列化程序。您尝试使用的JsonApiSerializer需要Newtonsoft。通过安装以下nuget软件包,可以在项目中使用Newtonsoft。(不要使用最新的5.0.0版本,因为它需要net5.0)

然后,您可以如下配置序列化程序。它不允许您替换整个
SerizlierSettings
,但是
JsonApiSerializerSettings
只设置了一些属性,因此我们可以直接设置它们

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddNewtonsoftJson(opts =>
    {
        opts.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
        opts.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
        opts.SerializerSettings.ContractResolver = new JsonApiContractResolver(new ResourceObjectConverter());
        opts.SerializerSettings.DateParseHandling = DateParseHandling.None;
    });
}
最后,您需要更新模型,使其包含
Id
字段。这就是
JsonApiSerializer
识别资源对象的方式

公共类CreateManufacturerBindingModel
{
公共字符串类型{get;set;}=“manufacturer”;
//需要有一个ID属性
公共字符串Id{get;set;}
公共字符串名称{get;set;}
公共分配ProductBidingModel[]产品{get;set;}
}

在这个位置上,它应该开始将对象序列化和反序列化为JsonApi对象

那么,您的模型应该有一个带有属性
数据的封闭对象
,这可能是一个问题?@JSteward-JsonApiSerializer库应该为您处理这个问题me@JSteward但是我想我没有把它设置正确。工作得很好!非常感谢你,亚历克斯!
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.10`
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddNewtonsoftJson(opts =>
    {
        opts.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
        opts.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
        opts.SerializerSettings.ContractResolver = new JsonApiContractResolver(new ResourceObjectConverter());
        opts.SerializerSettings.DateParseHandling = DateParseHandling.None;
    });
}