![](/img/trans.png)
[英]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.