ASP.NET Core 2.1中的FromRoute和FromBody模型绑定可以通过以下方式实现:
[ApiController]
[Route("api/[controller]")]
public class ExampleController : ControllerBase
{
[HttpPost("example/{id}")]
public IActionResult ExampleMethod([FromRoute] int id, [FromBody] ExampleModel model)
{
// 在这里可以使用id和model进行业务逻辑操作,并返回结果
return Ok();
}
}
public class ExampleModel
{
public string Name { get; set; }
public int Age { get; set; }
}
/api/example/1
,请求体中包含一个JSON对象:{
"name": "John",
"age": 25
}
通过以上步骤,你可以在ASP.NET Core 2.1中使用FromRoute和FromBody模型绑定。