How to transfer parameter information from the body in ASP.NET Web APi?

I am trying to pass a long string through my POST method from the actual body, it works fine if I pass it through the URL, but I do not know what to change, so I can insert data from the body.

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

if I use it as follows:

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

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

enter image description here

+4
source share
2 answers

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

EDIT:

enter image description here

+4
source

Use the hidden field and set its value before the message. Just make sure it is inside your form.

 @Html.HiddenFor(Model.data) 

To set a value in javascript using jQuery:

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

Alternatively, if you are not attached to a strongly typed view, instead of @Html.HiddenFor() simple, hidden HTML code will work:

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

Source: https://habr.com/ru/post/1490539/


All Articles