我需要设置一个 Web API 方法来接受从我的 Android 和 iOS 客户端应用程序发送的 POST 参数。这就是我现在拥有的:
[HttpPost]
[Route("api/postcomment")]
public IHttpActionResult PostComment([FromBody]string comment, [FromBody]string email, [FromBody]string actid)
{
string status = CommentClass.PostNewComment(comment, email, actid);
return Ok(status);
}
但是这不起作用,因为我相信该方法不能同时采用多个 [FromBody] 参数?如何正确设置此方法,使其接受请求正文中的 3 个 POST 参数?
最佳答案
您可以使用模型。 DefaultModelBinder 会将这些值从表单绑定(bind)到您的模型。
public class CommentViewModel
{
public string Comment { get; set; }
public string Email { get; set; }
public string Actid { get; set; }
}
public IHttpActionResult PostComment([FromBody]CommentViewModel model)
{
string status = ...;
return Ok(status);
}
关于asp.net - 设置 POST Web API 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31477108/