以下是一个使用Asp.Net Core Web API进行模型绑定的示例代码:
public class User
{
public int Id { get; set; }
public string Name { get; set; }
}
[HttpPost]
public IActionResult Post([FromBody] User user)
{
// 进行处理或保存User对象
// ...
return Ok();
}
using (HttpClient client = new HttpClient())
{
User user = new User { Id = 1, Name = "John Doe" };
string json = JsonConvert.SerializeObject(user);
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync("https://example.com/api/user", content);
// 处理响应
// ...
}
以上代码演示了如何在Asp.Net Core Web API中进行模型绑定。可以根据具体需求进行适当的修改和扩展。
上一篇:ASP.NET Core Web API的HttpPost方法使用charset1252编码
下一篇:ASP.NET Core Web API的POST请求在Postman中可以工作,但在C#/React中无法工作。