How ToT2 Dark_1200x303

You have multiple options for passing multiple parameters to a GET method: FromRouteAttribute, FromQuery and Model Binding.

In this article, we are going to learn how to pass multiple parameters to a GET method. This article covers the following points:

  • What are the ways to pass multiple parameters to a GET method?
  • Passing multiple parameters using FromRouteAttribute
  • Passing multiple parameters using FromQuery
  • What is Model Binding?
  • Conclusion

How to Pass Multiple Parameters to a GET Method?

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

  • Using FromRouteAttribute
  • Using FromQuery

Passing Multiple Parameters Using FromRouteAttribute

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

Example:

//GET method
//single parameter
public IActionResult Get(int id)

//multiple parameter
[HttpGet("{id}/{backendOnly}")]
public IActionResult Get(int id, string backendOnly)

//multiple parameters
[HttpGet("{id}/{first}/{second}")]
public IActionResult Get(int id, string first, string second)
//GET request using route data
https://localhost:44363/2
https://localhost:44363/2/first
https://localhost:44363/2/first/second

Passing Multiple Parameters Using FromQuery

In this option the parameters are bound using the request query string.

Example:

//GET method
//single parameter
[HttpGet("details")]
public IActionResult Details(int id)

//multiple parameter
[HttpGet("details")]
public IActionResult Details(int id, string first)

//multiple parameters
[HttpGet("details")]
public IActionResult Details(int id, string first, string second)
//GET request using query parameters
https://localhost:44363/details?id=2
https://localhost:44363/details?id=2&&first=csharp
https://localhost:44363/details?id=2&&first=csharp&&second=mvc

What is Model Binding?

The model binding works with data coming from HTTP requests and passed to the controller and Razor pages as parameters. It works in the following ways:

  • It fetches data from various sources like query string, form fields and data routes
  • It provides data to controller methods as parameter
  • It converts data to .NET types
  • It updates properties of complex types

Model Binding Sources

It gets the data from various sources in the form of key-value pairs from the following sources:

  • Form fields
  • Request body
  • Route data parameters
  • Query string parameters
  • Uploaded files

To work with model binding, we need to use following attributes:

  • BindProperties attribute—This can be applied to class level to define the all properties need to map with HTTP request

    Example:
    [BindProperties]
    public class GetRequest
    {
        public int Id { get; set; }
        public string FrontEnd { get; set; }
        public string BackEnd { get; set; }
    }
    
  • BindProperty attribute—This can be applied in case you want to specify a particular property to be used

    Example:
    [BindProperty]
    public int Id { get; set; }
    

To work with complex types, the built-in attributes are available as follows:

  • BindRequired attribute—It causes the model binding to add a model state error if model property binding is not occuring
  • BindNever attribute—It prevents data binding to a particular model property
  • Bind attribute—It can be applied to a class or method parameters, and it specifies the available properties to bind in model

For example, we are passing multiple parameters to GET method but in case our parameters exceed three, then it’s not readable and best practice to use that way. We can use class model as a parameter to a GET method with model binding as follows:

// Model as parameter
[BindProperties]
public class GetRequest
{
   public int Id { get; set; }
   public string FrontEnd { get; set; }
   public string BackEnd { get; set; }
}
// GET method
[HttpGet]
public IActionResult GetAction(GetRequest getRequest)
https://localhost:44363/details?id=2&&first=csharp&&second=mvc

In the above example, you can see how we use model binding with GET action method and pass multiple parameters using query string, but model binding fetches data from it and binds to the model and passes that model to GET action method.

Example: Pass Multiple Parameters Using Both FromRouteAttribute, FromQuery

We can use both the options to pass multiple parameters to a GET method as follows:

// GET method
[HttpGet("id")]
public IActionResult BothDetails(int id, string backendOnly, string frontendOnly)
// GET request using both ways
https://localhost:44363/3?backendOnly=csharp&&frontendOnly=angular

In the above example, we pass one parameter using FromRouteAttribute and two parameters using FromQuery.

You can also download this example from here.

Conclusion

In this article, we discussed ways of passing multiple parameters to a GET method: FromRouteAttribute, FromQuery and Model Binding, using simple examples. If you have any suggestions or queries regarding this article, please contact me.

“Learn It, Share it.”


jeetendra
About the Author

Jeetendra Gund

Jeetendra Gund is a C# Corner MVP as well as the Chapter Leader of C# Corner Pune Chapter. He holds a master’s degree in Computer Science and has worked on several different domains. He has spent six years in grooming his knowledge about Microsoft Technologies and has been sharing his experiences on technologies such as C#.Net, MVC.Net, SQL Server, Entity Framework, AngularJS, JavaScript, HTML, and .NET Static Code Analysis. He is a regular speaker at C# Corner on Microsoft Technologies. Find him: C# Corner, LinkedIn, or Twitter

Related Posts

Comments

Comments are disabled in preview mode.