简体   繁体   中英

how to pass Parameters Info from the Body in ASP.NET Web APi?

I'm trying to pass a lengthy string through my POST methond from the actual body, it works perfectly fine if I pass it through url but I dont know what to change so I can insert data from body instead.

public void PostMethod(string id, [FromBody]string data)
    {
        if (ModelState.IsValid)
        {
            var result = client.Store(StoreMode.Add, id, data);
        }
        else
        {
        }

    }

if I use it like this:

http://localhost:8888/api/data?id=2&data=MybigString

It works perfectly, but I don't want to pass data from URL, any suggestion would be highly appreciated.

在此输入图像描述

Given your action method, which is public void PostMethod(string id, [FromBody]string data) , you can use the URI of http://localhost:8888/api/data/2 and the message body of =MyBigString . If you use jQuery, you can use something like this: $.post('api/data/2', { '': 'MyBigString' }); to ensure the correct message body is sent.

EDIT:

在此输入图像描述

Use a hidden field and set the value of it before the post. Just make sure it's inside your form.

@Html.HiddenFor(Model.data)

To set the value in javascript using jQuery:

$("#data").val('my big string');

Alternatively, if you're not binding to a strongly-typed view, a simple HTML hidden input will work instead of the @Html.HiddenFor() :

<input type="hidden" id="data" name="data" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM