asp.net - 设置 POST Web API 方法?

标签 asp.net web-services rest asp.net-web-api azure-mobile-services

我需要设置一个 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)到您的模型。

enter image description here

enter image description here

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/

相关文章:

c# - 获取不同语言的系统时区

web-services - Kerberos 身份验证和反向代理的相互作用

c# - 类型初始值设定项 "Class"引发异常。LoadLibrary 失败

rest - Apiary.io 和/或 API Blueprint 是否支持 API 版本控制?

rest - squashfs 文件应使用哪种 MIME 类型?

c# - C#中的VB.Net错误对象

c# - 如何在 ASP.NET 中创建资源管理器

Javascript - 使用 foreach 并在鼠标悬停时更改图像

javascript - 如何在 REST Web 服务中保留 javascript console.log(通过重载)?

rest - org.springframework.http.MediaType 中的无效 token 字符 '/'