Having problems in an ASP.NET Core Web API with using the FromBody or FromQuery attributes?
Maybe you are finding that some of the properties are returning null? Or maybe, you are being greeted with a 415 unsupported media type error.
With this example, we have written an ASP.NET Core Web API in C#, and have provided two controllers, with similar features.
Both controllers both have a HTTP GET request and a HTTP POST request, allowing us to send a JSON request.
However, there is one significant different between the two. One of them contains the ApiController attribute.
The ApiController attribute relies on a JSON request, so we tend to have an issue when we are not sending a JSON request to an API endpoint.
This example demonstrates when we need to use the FromBody attribute, and when we need to use the FromQuery attribute.
This is the software that will need to be installed onto your machine.
These are the steps to get the application working.
You are now ready to go.
RoundTheCode.FromBody.sln in Visual Studio.
You should find that the web application runs on
Start the project in Visual Studio.
You can now run a HTTP GET request (using querystring), and HTTP POST requests (using form-data, x-www-form-url-encoded and application/json content types) using
If you wish to run it in Postman, you can import the scripts from the
Postman folder in the source code provided.
To get a better understanding on how the FromBody and the FromQuery behave, read our article "Why is the ASP.NET Core FromBody not working or returning null?".
In-addition, watch our video tutorial. We demonstrate when our properties are returning null, or when we are getting a 415 error. We then go about fixing them.