我简直没有运气将邮递员的url编码的表单值发送到使用file-> new project创建的香草asp 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 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 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 Web API的默认模板.
Note again that this is the default template for asp 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核心控制器操作时,您现在必须指定其来源.可以推断出复杂的类型,但不能推断出诸如int和string之类的简单类型(如果它们位于消息正文中.如果它们位于查询字符串或route(url)中,则可以推断出它们).我看到在主体中传递值的两个选项是:
When posting simple types to an asp 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:
邮递员的请求:
POST your-url/api/values HTTP/1.1 Content-Type: application/x-www-form-urlencoded value=test动作方法签名:
[HttpPost] public void Post([FromForm]string value) { }邮递员的请求
POST 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无法正常工作
发布评论