Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net core MVC post parameter always null

I am new to MVC core.

I have created a project with MVC core which has a controller. This controller has Get and Post action methods. If i pass data to Get method using query string it works fine, but when i pass complex JSON to post method, then it always shows me null.

Here what i am doing:

Post Request

URL: http://localhost:1001/api/users
Content-Type: application/json
Body: 
{
   "Name":"UserName",
   "Gender":"Gender of the user",
   "PhoneNumber":"PhoneNumber of the user"
}

Here is the Post action method

[HttpPost]
[Route("api/users")]
public async Task<IActionResult> Post([FromBody]User newUser)
{
   ...
}

When post request is called, then newUser always shows me null. And if i remove [FromBody] attribute then i receive newUser object but all of its fields are null.

Please help me and guide me in this issue.

EDITED

Here is my User class

public class User{

   public int Id { get; set; }
   public string Name { get; set; }
   public string Gender { get; set; }
   public string PhoneNumber { get; set; }
}

I had done same as described here for json data, but still receives null.

like image 709
Ravi Patel Avatar asked Jul 19 '16 10:07

Ravi Patel


People also ask

Why your FromBody parameter is always null?

If you need to get multiple values from the request body, define a complex type. But still the value of email is NULL . The JavaScript code is part of generic method we use, so that's why the content-type is set to application/json; charset=utf-8 .

Can we use FromBody with Httpget?

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

How do I pass a null parameter in Web API?

Simply add parameterName = null in your route parameter. Another option is add an overload. Have 2 function names receive different parameters. @kanika it's a precaution because there might be something yet to be setup in your controller and your controller is not accepting the parameters being sent.

What is FromBody in asp net core?

[FromBody] attributeThe ASP.NET Core runtime delegates the responsibility of reading the body to an input formatter. Input formatters are explained later in this article. When [FromBody] is applied to a complex type parameter, any binding source attributes applied to its properties are ignored.


3 Answers

This could be because of how the null values are being handled. Set NullValueHandling to Ignore in AddJsonOptions and see if that works.

public void ConfigureServices(IServiceCollection services)
{
    services
        .AddMvc()
        .AddJsonOptions(jsonOptions=>
        {
            jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
        });
}
like image 129
bvoleti Avatar answered Sep 19 '22 17:09

bvoleti


Note the original method Post([FromBody] User newUser)

For future readers from google, this same issue could arise if the method was Post(User newUser)

Note the lack of [FromBody]. This is a departure from previous versions of MVC where these parameters were generally inferred.

If you're an existing MVC5 developer who finds this page regarding AspNetCore.MVC, make sure to double check that you have [FromBody] decorated where relevant.

like image 28
Chris Marisic Avatar answered Sep 22 '22 17:09

Chris Marisic


I created new ASP.NET Core project, added your functionality, and it works. Please, checkout this project on github.

Also, see screenshot of log with simple communication with this controller from browser console: Console output

like image 22
Roman Zinnatov Avatar answered Sep 22 '22 17:09

Roman Zinnatov