FromQuery? FromBody? What are these ASP.NET Core Web API attributes?

preview_player
Показать описание
FromQuery and FromBody are some of the attributes that can be placed on a parameter in an controller's action.

These attributes can be used in an ASP.NET Core Web API or MVC app and it helps to bind a parameter against a value which is part of the HTTP request.

The attributes we will look at are:

► FromQuery - Binds a parameter from the query string
► FromHeader - Binds a parameter from the header
► FromForm - Binds a parameter from a form field if the multipart/form-data or x-www-url-encoded content type is used in the request
► FromRoute - Binds a parameter from a route attribute
► FromBody - Binds a parameter when the application/json content type is used in the request and JSON formatted content is part of the body

There is also a FromServices attribute. However, this binds an instance that has been added to the IoC container as part of dependency injection.

This video will show you a C# example of each one and how to test it using Postman.

In-addition, we'll have a look at the ApiController attribute and how the features and behaviour of a controller are changed with this attribute.

💻 Download the code example for this tutorial💻

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

► More information:

► Chapters
0:00 The ASP.NET Core attributes we will use
0:19 Learn with .NET courses
0:29 Create controller
1:12 FromQuery attribute
3:01 FromHeader attribute
5:09 FromForm attribute
7:11 FromRoute attribute
9:12 FromServices attribute
12:23 FromBody attribute
14:57 What's included with the code sample

#webapi #mvc #dotnet
Рекомендации по теме
Комментарии
Автор

Great content. keep up the good work. Cheers.

DarshanaHashendra
Автор

This is really helpfull.. Could you expand on this and include IFroms and IFormCollections or how to upload files with DTO

arsbkhan
Автор

Question: when we use [fromquery] but i need to recive more than one parameters, do a have to create a class? or in a method i can declare [fromquery] many time?

henryespinozat
Автор

hello good afternoon David,

I have a doubt, how can I specify a specific Content-Type ? for example in one end point I want to receive an application/json and in another one a text/plain.

oscarlemdi