Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.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
C# .Net Core 1.1 Web API Json输入/输出格式化程序_C#_Asp.net Core Webapi_Asp.net Core 1.1 - Fatal编程技术网

C# .Net Core 1.1 Web API Json输入/输出格式化程序

C# .Net Core 1.1 Web API Json输入/输出格式化程序,c#,asp.net-core-webapi,asp.net-core-1.1,C#,Asp.net Core Webapi,Asp.net Core 1.1,我正在构建一个Web API,在其中我可以在启动类中添加输入/输出格式化程序。这适用于XML,但不适用于Json。我知道默认情况下会添加Json,但如果没有指定Accept头,它似乎会选择XML public void ConfigureServices(IServiceCollection services) { //Add framework services. services.AddMvc(options => { options.RequireHt

我正在构建一个Web API,在其中我可以在启动类中添加输入/输出格式化程序。这适用于XML,但不适用于Json。我知道默认情况下会添加Json,但如果没有指定Accept头,它似乎会选择XML

public void ConfigureServices(IServiceCollection services)
{
   //Add framework services.
   services.AddMvc(options =>
   {
       options.RequireHttpsPermanent = true;
       options.RespectBrowserAcceptHeader = true;
       options.ReturnHttpNotAcceptable = true;
       options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
       options.InputFormatters.Add(new XmlDataContractSerializerInputFormatter());
     });

    services.AddDbContext<CustomerManagerContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))
    );

    services.AddScoped<IUnitOfWork, UnitOfWork>();
}

默认格式化程序是添加到格式化程序列表中的第一个格式化程序。我想在XML之前添加Json格式化程序,使其成为默认设置。我错过了什么?如何正确添加Json格式化程序,使其成为格式化程序列表中的第一个?

我假设请求的Accept头是为XML而不是Json设置的。只有当请求的Accept头指定了一种格式时,才会进行协商,否则使用默认格式(默认为JSON),如果无法满足请求,则由服务器决定


如果您一直在获取XML,我猜消费者的请求是在请求XML。

我假设请求的Accept标头设置为XML而不是JSON。只有当请求的Accept头指定了一种格式时,才会进行协商,否则使用默认格式(默认为JSON),如果无法满足请求,则由服务器决定

如果您一直在获取XML,我猜消费者的请求就是在请求XML

options.OutputFormatters.Add(new JsonOutputFormatter());