Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.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# HTTP POST中ASP.Net核心供应商特定的Mime类型_C#_Asp.net Core - Fatal编程技术网

C# HTTP POST中ASP.Net核心供应商特定的Mime类型

C# HTTP POST中ASP.Net核心供应商特定的Mime类型,c#,asp.net-core,C#,Asp.net Core,为了使模型绑定器正确绑定ASP.NETCore中HTTP请求的主体部分,您需要使用[FromBody]属性 public JsonResult PostContent([FromBody] Content content) { ...... } 如果您的媒体类型是application/json,那么这种方法非常有效。但是,如果您想在Accept标题中使用特定于供应商的媒体类型(例如application/vnd+mycompany+json),那么如果我在HTTP POST中传递该类

为了使模型绑定器正确绑定ASP.NETCore中HTTP请求的主体部分,您需要使用
[FromBody]
属性

public JsonResult PostContent([FromBody] Content content)
{
    ......
}
如果您的媒体类型是
application/json
,那么这种方法非常有效。但是,如果您想在
Accept
标题中使用特定于供应商的媒体类型(例如
application/vnd+mycompany+json
),那么如果我在HTTP POST中传递该类型,我将获得
415不支持的媒体类型

所以我的问题是如何在ASP.Net Core中支持特定于供应商的媒体类型?

我就是这样做的

Startup.cs中

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IApiInfoService, ApiInfoService>();
    services.AddTransient<IApiVersion, ApiVersion>();
    services.AddTransient<IContentService, ContentService>();
    services.AddTransient<IIdGenerator, GuidIdGenerator>();

    // Add framework services.
    services.AddMvc(
        mvcConfig => {
            mvcConfig.InputFormatters.OfType<JsonInputFormatter>().First().SupportedMediaTypes.Add(
                MediaTypeHeaderValue.Parse(ContentTypes.VENDOR_MIME_TYPE)
            );
        }
    );
}
public void配置服务(IServiceCollection服务)
{
services.AddTransient();
services.AddTransient();
services.AddTransient();
services.AddTransient();
//添加框架服务。
services.AddMvc(
mvcConfig=>{
mvcConfig.InputFormatters.OfType().First().SupportedMediaTypes.Add(
MediaTypeHeaderValue.Parse(ContentTypes.VENDOR\u MIME\u类型)
);
}
);
}

检查文档中提到的自定义格式化程序。