ASP.NET Core FromBody: Not working? Or returning null? (Solution)

preview_player
Показать описание
The ASP.NET Core FromBody. Is it not working? Or returning null? This video will provide a solution to this common .NET problem.

We've made a JSON request to an API endpoint. But, two potential issues are arising. Maybe our request is not binding properly and returning null? Or maybe, we are getting a 415 unsupported media type error?

In this video, we will look at two ways on how to set up a controller for an ASP.NET Core Web API.

Using C#, we will write some API endpoints for a HTTP GET request and a HTTP POST request.

Afterwards, we will run the application and perform the HTTP GET request using a querystring. In addition, we will do three HTTP POST requests using the following content types:

- form-data
- x-www-form-url-encoded
- application/json

We will demonstrate when we need to include the FromBody attribute, and when we need to use the FromQuery attribute.

In addition, we will explore the differences in behaviour when we use the ApiController attribute.

You will find that it behaves differently with a JSON request when using the ApiController attribute.

💻 Download the code example for this demo 💻

📖 Learn .NET and C# with our online courses 📖

#aspnetcore #frombody #fromquery
Рекомендации по теме
Комментарии
Автор

This can't be better, Getting a video on the exact problem I am facing uploaded in the exact same day

v_iancu
Автор

Você merece um premeio amigo, tava sofrendo pra conseguir receber os valores por json vinha sempre vazio já estava perdendo as forças quando achei seu video algo tão simples mas não estava achando em lugar nenhum ! Muito obrigado você é o cara !

Squirrell_LTS
Автор

Also worth mentioning. In my case I was using a ApiController, sent JSON to the endpoint but model was still null. Checking the "ModelState"-object to see if there is any errors gave me insight into way my model was null. I hade a none nullable DateTime on the DTO but no value was provided which resulted in an error so the whole model was null. "Back in the days" the model binder would have set the DateTime to DateTime.Min but now it just did not work.

markusjohansson
Автор

Keep up the good work! Greetings from Hungary!

nyadpics
Автор

Thank you Sir. It was short and simple

roiram
Автор

if you try to bind a string to decimal it also fails to bind and returns null.
Took me some time and pain to notice.

nurettinselcuk
Автор

Hi...should we implement validateantiforgerytoken when already implement identity ??

wisnu
Автор

There is an issue I'm having now I have a string route parameter and an object parameter from body. I'm trying to bind string parameter to object but just couldn't do it. I've read a issue on .net core it seems like they'll handle it on .net 8

yasincoskun
Автор

con estas líneas me solució el problema: dentro de ConfigureServices
=>
=
);

jimmyjaramillo