在正文中将int/string(简单类型)发布到asp.net核心Web API 2.1无法正常工作 [英] Post with int/string (simple type) in body to asp.net core web api 2.1 not working

查看:57
本文介绍了在正文中将int/string(简单类型)发布到asp.net核心Web API 2.1无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我简直没有运气将邮递员的url编码的表单值发送到使用file-> new project创建的香草asp.net core 2.1 Web api.我什么都没做,但是新的模型验证功能似乎仍然起作用,并向邮递员返回了400 Bad Request.谁能告诉我我在做什么错?

I'm simply having no luck sending an url encoded form value from postman to a vanilla asp.net core 2.1 web api created with file->new project. I do nothing to it whatsoever but still the new model validation feature seems to kick in and returns a 400 Bad Request to postman. Can anyone tell me what I'm doing wrong?

控制器操作:

// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}

原始请求(如提琴手所示):

Raw request (as seen in fiddler):

POST http://localhost:60843/api/values HTTP/1.1
Content-Type: application/x-www-form-urlencoded
cache-control: no-cache
Postman-Token: a791eee7-63ff-4106-926f-2s67a8dcf37f
User-Agent: PostmanRuntime/7.3.0
Accept: */*
Host: localhost:60843
accept-encoding: gzip, deflate
content-length: 7
Connection: keep-alive

value=test

原始响应:

HTTP/1.1 400 Bad Request
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Kestrel
X-SourceFiles: =?UTF-8?BQzpcUmVwb3NcVGVzdGJlZFxNb2RlbEJpbmRpbmdcTW9kZWxCaW5kaW5nXGFwaVx2YWx1ZXM=?=
X-Powered-By: ASP.NET
Date: Thu, 25 Oct 2018 15:23:49 GMT

21
{"":["The input was not valid."]}
0

再次请注意,这是Visual Studio 2017中asp.net Web API的默认模板.

Note again that this is the default template for asp.net web api in Visual Studio 2017.

一个有趣的事实是,如果我添加Swashbuckle并转到swagger ui端点并使用内置的"try it"功能,那么开箱即用也会产生错误.

An interesting fact is that if I add Swashbuckle and go to the swagger ui endpoint and use the built in "try it" functionality it produces an error as well, out of the box.

我已经将其用于复杂类型和json主体,但是我无法使用简单类型,因此我尝试了各种不同的内容类型.

I've gotten this to work with a complex type and a json body, but I can not get the simple type to work and I've tried with all sorts of different content-types.

推荐答案

对于那些偶然发现此问题的人,默认情况下从2.1版开始自动应用模型绑定,如果模型绑定失败,则返回400错误请求(更改为早期版本,需要您检查ModelState.IsValid以查看模型绑定是否成功).

For those who stumble upon this question, starting with version 2.1 automatic model binding is applied by default and a 400 Bad Request is returned if model binding fails (change from earlier versions which required you to check ModelState.IsValid to see if model binding was successful).

现在将简单类型发布到asp.net核心控制器操作时,您现在必须指定其来源.可以推断出复杂的类型,但不能推断出诸如int和string之类的简单类型(如果它们位于消息正文中.如果它们位于查询字符串或route(url)中,则可以推断出它们).我看到在主体中传递值的两个选项是:

When posting simple types to an asp.net core controller action you now have to specify where it is coming from. Complex types can be inferred but simple types like int and string cannot (if they're in the message body that is. They are inferred if they're in either the query string or route(url)). The two options I see to pass values in the body itself are:

  1. 通过url编码的表单值(将 [FromForm] 添加到操作中的参数)
  1. By way of url encoded form value (add [FromForm] to your parameter in the action)

邮递员的请求:

POST http://your-url/api/values HTTP/1.1
Content-Type: application/x-www-form-urlencoded

value=test

动作方法签名:

[HttpPost]
public void Post([FromForm]string value)
{
}

  1. 通过json正文(将 [FromBody] 添加到操作中的参数)
  1. By way of a json body (add [FromBody] to your parameter in the action)

邮递员的请求

POST http://your-url/api/values HTTP/1.1
Content-Type: application/json

"test"

动作方法签名:

[HttpPost]
public void Post([FromBody]string value)
{
}

这篇关于在正文中将int/string(简单类型)发布到asp.net核心Web API 2.1无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆