FromQuery and other ASP.NET Core Web API attributes

FromQuery. FromHeader. FromForm. FromRoute. FromServices. FromBody.

These are all attributes that can be added to an action parameter in an ASP.NET Core controller. These work regardless of whether you are building an ASP.NET Core Web API or MVC app.

What's included?

The code sample will give you examples of how to use these attributes in an ASP.NET Core web app as part of a controller's action. They also include a sample of using the Name property which are included in a number of these attributes.

C# coding challenges

C# coding challenges

Our online code editor allows you to compile the answer.

In-addition, we'll give you a Postman import script so you can execute these endpoints.

Watch our video to see the controllers are included, how the functionality works and how we tested it in Postman.

As well as that, you can read the tutorial so you can understand how each attribute works and when to use them.

Software

This is the software that will need to be installed onto your machine.

Get the application working

These are the steps to get the application working.

  • Fill out the code example form. We will send you an email where you can download the code example.

You are now ready to go.

Open the project in Visual Studio

Open up RoundTheCode.FromAttributes.sln in Visual Studio. The start up project should be set to RoundTheCode.FromAttributes.Web which is the ASP.NET Core web app.

Start the application, and your browser should open up to https://localhost:9906 with the Swagger documentation.

At this point, you'll be able to execute each endpoint in Postman.

Import Postman endpoints

Inside the Postman folder of the ZIP file, there are two Postman import scripts. Assuming you are using a later version of Postman, you should be able to use the RoundTheCode.FromAttributes.postman_collection_2.1.json file to import the endpoints to Postman.

In Postman, click on the menu icon in the top-left hand corner of the screen and go to File and Import.

How to import a Postman script

How to import a Postman script

Click on the files link and find the Postman import file.

Select a Postman file to import

Select a Postman file to import

The endpoints should now be imported into Postman. With your ASP.NET Core web app running, you should be able to execute each endpoint.

Imported ASP.NET Core endpoints in Postman

Imported ASP.NET Core endpoints in Postman