Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FromBody value get null

This is Asp.Net Webform application
This is my POST method in my Apicontroller

public void Post([FromBody]string value)
{
}

I'm with fiddler post process.
I did so experiment.
But it did not.

What is the problem.
Can you help?

I've tried it, I've failed.

public void Post(MyViewModel model)
{
   string aa = model.Value;
}

public class MyViewModel
{
   public string Value { get; set; }
}

In Fiddler:

Request Body:   
Value=hakan
like image 760
Hakan Ertuğ Avatar asked May 30 '13 13:05

Hakan Ertuğ


People also ask

Why your FromBody parameter is always null?

Web API reads the response body at most once, so only one parameter of an action can come from the request body. If you need to get multiple values from the request body, define a complex type. But still the value of email is NULL .

Can we use FromBody in HTTP GET?

Please note that we are able to send [FromBody] parameter in HTTP GET Request input.

What does FromBody mean in C#?

The [FromBody] attribute which inherits ParameterBindingAttribute class is used to populate a parameter and its properties from the body of an HTTP request. The ASP.NET runtime delegates the responsibility of reading the body to an input formatter.

What is difference between FromQuery and FromBody?

[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data. [FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.


2 Answers

The POST body payload in Fiddler should be:

=foo_bar

instead of:

value=foo_bar

That's just one of those strange things about the model binding in the Web API. If you want to support value=foo_bar in the POST body payload you could always write a view model:

public class MyViewModel
{
    public string Value { get; set; }
}

and then have your method take this view model as parameter:

public void Post(MyViewModel model)
{
    ... work with model.Value here as usual
}
like image 132
Darin Dimitrov Avatar answered Sep 30 '22 05:09

Darin Dimitrov


I meet this issue, and the solution is:
to fit the Request Body format =foo_bar,
also need Request haders:

Content-Type: application/x-www-form-urlencoded
like image 28
dasons Avatar answered Sep 30 '22 06:09

dasons