Pass Multiple Inputs to an HTTP GET in .NET

Today in this article, we shall see a simple and basic way to Pass Multiple Inputs to an HTTP GET in .NET.

ASP.NET Core API template gives you multiple ways to pass the input argument like using arguments within the route or the query.

We shall cover below aspects in this article,

Above discussed approach can be used in ASP.NET API or MVC project templates as required.

It’s important to follow the proper URL naming convention for REST API.

Getting started

Let’s create ASP.NET 6 API application,

There are multiple ways to pass multiple parameters to a method, but the following are the options mostly used for the GET method:

  • Using FromRouteAttribute

  • Using FromQuery

Pass Multiple Parameters to a GET API using Query Parameters

Example 1

https://localhost:44367/api/Accounts/PersonalAccounts?employeeId=123
         [HttpGet]
         [Route("PersonalAccounts")]
         public async Task GetAccountDetails(int employeeId)
         {

Example 2:

https://localhost:44367/api/Accounts/PersonalAccounts?employeeId=123&accountNumber=238

Pass Multiple Inputs to an HTTP GET

Example 3:

https://localhost:44367/api/Accounts/PersonalAccounts?employeeId=123&accountNumber=238&taxid
         [HttpGet]
         [Route("PersonalAccounts")]
         public async Task GetAccountDetails(string employeeId, int accountNumber, int taxId)
         {

         }

So with the query parameter, the input sequence gets matched with corresponding input parameters at the Controller method level.

Pass Multiple Parameters to a GET API using Inline Route Parameters

In this option, the parameters are bound using the route data from the current request.

Here we make use of the attribute [FromRoute] to indicate the parameter to be picked up from the inline route parameter.

The parameter provided using [FromRoute] should exactly match the input argument from the route.

Example 1:

https://localhost:44367/api/Accounts/PersonalAccounts/203050
         [HttpGet]
         [Route("CheckingAccounts")]
         public async Task GetChkAccountDetails(int employeeId)
         {
         }

Example 2:

https://localhost:44367/api/Accounts/CheckingAccounts/2040/123123

         [HttpGet]
         [Route("CheckingAccounts/{employeeId}/{accountNumber}")]
         public async Task GetChkAccountDetails([FromRoute] int employeeId, [FromRoute] int accountNumber)
         {
         }

Please note that there are a few limitations on the size of input data that can be passed to the HTTP GET method as per specification.

Please read the below article to understand the same if interested,

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Happy Coding !!

Summary

Today in this article, we learned different ways of passing multiple parameters to a GET method using  FromRouteAttribute, and FromQuery inbuilt supported annotation in ASP.NET Core.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *