Post int/string(简单类型)正文到 asp.net 核心网络 api 2.1 不工作
Post with int/string (simple type) in body to asp.net core web api 2.1 not working
我只是运气不好,无法将 url 编码的表单值从邮递员发送到使用 file->new 项目创建的 vanilla asp.net core 2.1 web api。我什么也没做,但新的模型验证功能似乎仍然启动并且 returns 向邮递员发出 400 Bad Request。谁能告诉我我做错了什么?
控制器操作:
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
原始请求(如 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 的默认模板。
一个有趣的事实是,如果我添加 Swashbuckle 并转到 swagger ui 端点并在 "try it" 功能中使用 built,它也会产生错误,out盒子的。
我已经可以使用复杂类型和 json 主体,但我无法使用简单类型,我尝试了各种不同的内容类型。
对于那些偶然发现这个问题的人,从 2.1 版开始默认应用自动模型绑定,如果模型绑定失败则返回 400 Bad Request(与早期版本不同,需要您检查 ModelState.IsValid查看模型绑定是否成功)。
将简单类型发布到 asp.net 核心控制器操作时,您现在必须指定它的来源。可以推断出复杂类型,但不能推断出像 int 和 string 这样的简单类型(如果它们在消息正文中。如果它们在查询字符串或路由(url)中,则会推断出它们)。我看到在正文中传递值的两个选项是:
- 通过 url 编码形式值(将 [FromForm] 添加到操作中的参数中)
邮递员请求:
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)
{
}
- 通过 json 正文(将 [FromBody] 添加到操作中的参数中)
来自邮递员的请求:
POST http://your-url/api/values HTTP/1.1
Content-Type: application/json
"test"
操作方法签名:
[HttpPost]
public void Post([FromBody]string value)
{
}
我只是运气不好,无法将 url 编码的表单值从邮递员发送到使用 file->new 项目创建的 vanilla asp.net core 2.1 web api。我什么也没做,但新的模型验证功能似乎仍然启动并且 returns 向邮递员发出 400 Bad Request。谁能告诉我我做错了什么?
控制器操作:
// POST api/values
[HttpPost]
public void Post([FromBody] string value)
{
}
原始请求(如 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 的默认模板。
一个有趣的事实是,如果我添加 Swashbuckle 并转到 swagger ui 端点并在 "try it" 功能中使用 built,它也会产生错误,out盒子的。
我已经可以使用复杂类型和 json 主体,但我无法使用简单类型,我尝试了各种不同的内容类型。
对于那些偶然发现这个问题的人,从 2.1 版开始默认应用自动模型绑定,如果模型绑定失败则返回 400 Bad Request(与早期版本不同,需要您检查 ModelState.IsValid查看模型绑定是否成功)。
将简单类型发布到 asp.net 核心控制器操作时,您现在必须指定它的来源。可以推断出复杂类型,但不能推断出像 int 和 string 这样的简单类型(如果它们在消息正文中。如果它们在查询字符串或路由(url)中,则会推断出它们)。我看到在正文中传递值的两个选项是:
- 通过 url 编码形式值(将 [FromForm] 添加到操作中的参数中)
邮递员请求:
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)
{
}
- 通过 json 正文(将 [FromBody] 添加到操作中的参数中)
来自邮递员的请求:
POST http://your-url/api/values HTTP/1.1
Content-Type: application/json
"test"
操作方法签名:
[HttpPost]
public void Post([FromBody]string value)
{
}