- Home
- .NET tutorials
- Why is the ASP.NET Core FromBody not working or returning null?
Why is the ASP.NET Core FromBody not working or returning null?
Published: Wednesday 21 April 2021
There can be issues when using the FromBody attribute in an ASP.NET Core Web API.
Setting up our request
For our demonstration, we are going to setup an ASP.NET Core Web API application using C#.
// Customer.cs
public class Customer
{
public string Firstname { get; set; }
public string Surname { get; set; }
}
Setting up and testing endpoints using different methods
Next, we are going to set up a web controller.
/web.
Customer model as a parameter. We will return the Customer model as a response to see if the request has managed to populate our Customer model instance.
Customer model instance as a request, and return it as a response.
// WebController.cs
[Route("web")]
public class WebController
{
[HttpGet]
public Customer Get(Customer customer)
{
return customer;
}
[HttpPost]
public Customer Post(Customer customer)
{
return customer;
}
}
Now that we've done that, we are going to run some tests in Postman. We are running our ASP.NET Core Web API on https://localhost:7000.
| URL | Request method | Request body | Response code | Response |
|---|---|---|---|---|
https://localhost:7000/web |
GET (querystring) | firstname=David& |
200 | { |
https://localhost:7000/web |
POST (form-data) |
firstname=David |
200 | { |
https://localhost:7000/web |
POST (x-www-form-url-encoded) |
firstname=David |
200 | { |
https://localhost:7000/web |
POST (application/json) |
{ |
200 | { |
The application/json request isn't working quite right?
The good news is that all our requests are returning a 200 response!
Customer model are not being set. They are still at their default value of null.
[FromBody] attribute to our Customer model instance in our parameter. We do that for the action that is performing our HTTP POST request.
// WebController.cs
public class WebController
{
...
[HttpPost]
public Customer Post([FromBody] Customer customer)
{
return customer;
}
}
Now, that's run our three HTTP POST tests again with the [FromBody] attribute to see how they respond.
| URL | Request method | Request body | Response code | Response |
|---|---|---|---|---|
https://localhost:7000/web |
POST (form-data) |
firstname=david |
415 | (unsupported media type) |
https://localhost:7000/web |
POST (x-www-form-url-encoded) |
firstname=david |
415 | (unsupported media type) |
https://localhost:7000/web |
POST (application/json) |
{ |
200 | { |
When we run a POST request using either form-data or x-www-form-url-encoded content type, it now throws a 415 unsupported media type error.
Customer model type with the properties we sent in the request.
This is not how it's behaving?
When setting up a controller, there might be a case that the controller is not behaving in the same way when doing the above tests.
[ApiController] attribute attached to the controller.
WeatherForecast controller. Within this controller, it contains the [ApiController] attribute.
[ApiController] makes the controller behave in a different way when binding parameters from the request.
WebController we set up earlier. However, this time, we are going to add the [ApiController] attribute to the controller.
/webapi.
// WebApiController.cs
[ApiController]
[Route("webapi")]
public class WebApiController : Controller
{
[HttpGet]
public Customer Get(Customer customer)
{
return customer;
}
[HttpPost]
public Customer Post(Customer customer)
{
return customer;
}
}
Now, that's go ahead and run the same tests again to see what results we get.
| URL | Request method | Request body | Response code | Response |
|---|---|---|---|---|
https://localhost:7000/webapi |
GET (querystring) | firstname=David& |
415 | (unsupported media type) |
https://localhost:7000/webapi |
POST (form-data) |
firstname=David |
415 | (unsupported media type) |
https://localhost:7000/webapi |
POST (x-www-form-url-encoded) |
firstname=David |
415 | (unsupported media type) |
https://localhost:7000/webapi |
POST (application/json) |
{ |
200 | { |
Fixing the HTTP GET request
Interestingly, the HTTP GET request using a querystring is throwing a 415 error, alongside the form-data and x-www-form-url-encoded content types in the HTTP POST request.
[FromQuery] attribute to the parameter like so.
// WebApiController.cs
public class WebApiController : Controller
{
[HttpGet]
public Customer Get([FromQuery] Customer customer)
{
return customer;
}
...
}
Now that's rerun our HTTP GET request again and see what the result is.
| URL | Request method | Request body | Response code | Response |
|---|---|---|---|---|
https://localhost:7000/webapi |
GET (querystring) | firstname=David& |
200 | { |
The [FromQuery] attribute has now fixed our HTTP GET request.
What to do if building an API controller?
When building an API controller, the best bet is to include the [ApiController] attribute for the controller.
[ApiController] attribute for the controller.
[FromBody] attribute will not be required on a POST request using JSON.
[FromQuery] attribute if we are performing a HTTP GET request through the querystring.
Related tutorials
Why Blazor Wasm is the best choice for API integration
Why you should use Blazor WebAssembly over JavaScript frameworks for API integration. Examples of integrating a CRUD API in Blazor Wasm.