Rua*_*uan 15 c# api asp.net-core
我有一个带有一些测试功能的.net Core 2 API设置.(Visual Studio 2017)
使用postman我用一个原始数据发布到该方法的帖子,但模型只是空白?为什么?
// POST api/Product/test
[HttpPost]
[Route("test")]
public object test(MyTestModel model)
{
try
{
var a = model.SomeTestParam;
return Ok("Yey");
}
catch (Exception ex)
{
return BadRequest(new { message = ex.Message });
}
}
public class MyTestModel
{
public int SomeTestParam { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Kir*_*kin 25
您需要[FromBody在模型中包含]属性:
[FromBody] MyTestModel model
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅Andrew Lock的帖子:
为了在ASP.NET Core中正确绑定JSON,您必须修改操作以
[FromBody]在参数中包含该属性.这告诉框架使用请求的内容类型头来决定将哪个配置的IInputFormatters用于模型绑定.
正如@anserk在评论中所指出的,这也需要将Content-Type标题设置为application/json.
| 归档时间: |
|
| 查看次数: |
9573 次 |
| 最近记录: |