ASP.NET Core表单POST导致HTTP 415不支持的媒体类型响应

编程入门 行业动态 更新时间:2024-10-23 14:30:49
本文介绍了ASP.NET Core表单POST导致HTTP 415不支持的媒体类型响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

将表单POST HTTP请求(Content-Type: application/x-www-form-urlencoded)发送到以下控制器将导致 HTTP 415不支持的媒体类型响应.

Sending a form POST HTTP request (Content-Type: application/x-www-form-urlencoded) to the below controller results into a HTTP 415 Unsupported Media Type response.

public class MyController : Controller { [HttpPost] public async Task<IActionResult> Submit([FromBody] MyModel model) { //... } }

表单发布HTTP标头:

Form post HTTP headers:

POST /submit HTTP/1.1 Host: example:1337 Connection: keep-alive Content-Length: 219 Pragma: no-cache Cache-Control: no-cache Origin: example:1337 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Content-Type: application/x-www-form-urlencoded Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Referer: example:1337/submit Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.8,nl;q=0.6

这曾经与.NET 4.6上的ASP.NET MVC 5一起使用.

This used to work with ASP.NET MVC 5 on .NET 4.6.

推荐答案

对于表单,请使用 [FromForm] 属性而不是[FromBody]属性.

For forms, use the [FromForm] attribute instead of the [FromBody] attribute.

以下控制器可与ASP.NET Core 1.1配合使用:

The below controller works with ASP.NET Core 1.1:

public class MyController : Controller { [HttpPost] public async Task<IActionResult> Submit([FromForm] MyModel model) { //... } }

注意:如果控制器用[ApiController]注释,则需要[FromXxx].对于普通视图控制器,可以将其省略.

Note: [FromXxx] is required if your controller is annotated with [ApiController]. For normal view controllers it can be omitted.

更多推荐

ASP.NET Core表单POST导致HTTP 415不支持的媒体类型响应

本文发布于:2023-10-29 10:10:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539423.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不支持   表单   类型   媒体   ASP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!