[英]ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response
[英]ASP.Net Core Vendor Specific Mime Type in HTTP POST
为了使模型绑定器正确绑定ASP.Net Core中HTTP请求的正文部分,您需要使用[FromBody]
属性。
public JsonResult PostContent([FromBody] Content content)
{
......
}
如果您的媒体类型为application/json
则此方法非常application/json
。 但是,如果您想在Accept
标头中使用特定于供应商的媒体类型(例如application/vnd+mycompany+json
),那么如果我在HTTP POST中传递该415 Unsupported Media Type
则会得到415 Unsupported Media Type
。
所以我的问题是,您如何在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)
);
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.