在ASP.NET Core 3.1中,可以使用以下步骤将JSON请求属性绑定到操作方法参数模型:
[HttpPost]
public IActionResult MyAction([FromBody] MyModel model)
{
// 通过模型参数执行操作
// ...
}
public class MyModel
{
public string Property1 { get; set; }
public int Property2 { get; set; }
}
确保JSON请求的Content-Type标头设置为"application/json"。
发送包含与模型类属性相匹配的属性的JSON请求。例如:
{
"Property1": "Value1",
"Property2": 123
}
通过执行上述步骤,ASP.NET Core 3.1将自动将JSON请求属性绑定到操作方法参数模型中的相应属性。你可以在操作方法中使用该模型执行进一步的操作。