将表单POST HTTP请求(Content-Type: application/x-www-form-urlencoded)发送到以下控制器将导致 HTTP 415不支持的媒体类型 响应。
Content-Type: application/x-www-form-urlencoded
public class MyController : Controller { [HttpPost] public async Task<IActionResult> Submit([FromBody] MyModel model) { //... } }
表单发布HTTP标头:
POST /submit HTTP/1.1 Host: example.com:1337 Connection: keep-alive Content-Length: 219 Pragma: no-cache Cache-Control: no-cache Origin: https://example.com: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: https://example.com: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一起使用。
对于表单,请使用 [FromForm] 属性而不是[FromBody]属性。
[FromForm]
[FromBody]
以下控制器可与ASP.NET Core 1.1配合使用:
public class MyController : Controller { [HttpPost] public async Task<IActionResult> Submit([FromForm] MyModel model) { //... } }
注意:[FromXxx]如果您的控制器带有注释,则为必填项[ApiController]。对于普通视图控制器,可以将其省略。
[FromXxx]
[ApiController]