问题
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?
Controller Action:
// 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
Raw response:
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
Note again that this is the default template for asp.net web api in Visual Studio 2017.
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.
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.
回答1:
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).
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:
- By way of url encoded form value (add [FromForm] to your parameter in the action)
Request from postman:
POST http://your-url/api/values HTTP/1.1
Content-Type: application/x-www-form-urlencoded
value=test
Action method signature:
[HttpPost]
public void Post([FromForm]string value)
{
}
- By way of a json body (add [FromBody] to your parameter in the action)
Request from Postman:
POST http://your-url/api/values HTTP/1.1
Content-Type: application/json
"test"
Action method signature:
[HttpPost]
public void Post([FromBody]string value)
{
}
来源:https://stackoverflow.com/questions/52993193/post-with-int-string-simple-type-in-body-to-asp-net-core-web-api-2-1-not-worki