如何ApiController检索POST主体数据? [英] How to retrieve POST body data in ApiController?

查看:135
本文介绍了如何ApiController检索POST主体数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net 4.5和最新的MVC版本。我需要张贴在HTML页面表单时从头部retrive DEVICEID 。在行公共无效后([FromBody]字符串值)值总是空。

什我是做错了?

 命名空间WebApi.Controllers
    {
        公共类NotificationController:ApiController
        {
            //获取API /通知
            公共IEnumerable的<串GT;得到()
            {
                返回新的字符串[] {值1,值2};
            }            //获取API /通知/ 5
            公共字符串GET(INT ID)
            {
                回到价值;
            }            // POST API /通知
            公共无效后([FromBody]字符串值)
            {
//值时,它总是空
            }            // PUT API /通知/ 5
            公共无效认沽(INT ID,[FromBody]字符串值)
            {
            }            //删除API /通知/ 5
            公共无效删除(INT ID)
            {
            }
        }
    }
<形式的行动=/ API /通知的方法=后>
    DEVICEID:其中,输入名称=的DeviceID值=gateeMachine>
    体:其中,输入名称=身体值=警告缺纸>
    <按钮和GT;维护设备发送< /按钮>
< /表及GT;


解决方案

模型绑定起的名字在窗体属性的参数值相匹配。我觉得它不太头疼创建模型的,虽然(以下是未经考验code):

 公共类设备
{
     公共字符串DEVICEID {搞定;组; }
     公共字符串车身{搞定;组; }
}

该行动:

 公共无效后(设备装置)
{}

形式:

 <形式的行动=/ API /通知的方法=后>
    设备ID:<输入名称=的DeviceID值=gateeMachine>
    体:其中,输入名称=身体VALUE =警告缺纸>
    <按钮和GT;测试发送和LT; /按钮>
< /表及GT;

- 编辑:原来,结合多个表单值不支持(请参见回答),所以该模型是要走的路。你可以序列化表单值到查询字符串。你也可以序列化值到一个字符串(即JSON或XML),然后反序列化服务器端的,但实际上,该模型是最直接的路径在这里。

I am using asp.net 4.5 and latest MVC version. I need retrive deviceId and body from header when posting the form in the html page. At line public void Post([FromBody]string value) value it is always null.

Wha am I doing wrong here?

  namespace WebApi.Controllers
    {
        public class NotificationController : ApiController
        {
            // GET api/notification
            public IEnumerable<string> Get()
            {
                return new string[] { "value1", "value2" };
            }

            // GET api/notification/5
            public string Get(int id)
            {
                return "value";
            }

            // POST api/notification
            public void Post([FromBody]string value)
            {
// value it is always null
            }

            // PUT api/notification/5
            public void Put(int id, [FromBody]string value)
            {
            }

            // DELETE api/notification/5
            public void Delete(int id)
            {
            }
        }
    }


<form action="/api/notification" method="post">
    DeviceId: <input name="deviceId" value="gateeMachine">
    Body: <input name="body" value="Warning Out of Paper">
    <button>Tes send</button>
</form>

解决方案

The model binder matches the parameters to the values from the name attributes in your form. I find it's less of a headache to create models though (the following is untested code):

public class Devices
{
     public string DeviceId { get; set; }
     public string Body { get; set; }
}

The Action:

public void Post(Devices device)
{

}

The form:

<form action="/api/notification" method="post">
    Device Id: <input name="DeviceId" value="gateeMachine">
    Body: <input name="Body" value="Warning Out of Paper">
    <button>Test send</button>
</form>

-- EDIT: It turns out that binding multiple form values isn't supported (see this answer), so the model is the way to go. You could serialize your form values into the querystring. You could also serialize the values into a single string (i.e. JSON or XML) and then deserialize server-side, but really, the model is the most straight forward path here.

这篇关于如何ApiController检索POST主体数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆