C# HTTP POST中ASP.Net核心供应商特定的Mime类型
为了使模型绑定器正确绑定ASP.NETCore中HTTP请求的主体部分,您需要使用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中传递该类
[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类型)
);
}
);
}
检查文档中提到的自定义格式化程序。