Routing with Multiple `Parameters` using ASP.NET MVC
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
ASP.NET MVC maps URL segments to controller action parameters through its routing system. The default route {controller}/{action}/{id} handles a single parameter, but many applications need multiple parameters in URLs — for example /Products/List/Electronics/1/50 for category, page, and page size. You can define custom route templates, use attribute routing, query strings, or a combination to pass multiple parameters to controller actions.
Default Route (Single Parameter)
The default route only handles one parameter (id). For multiple parameters, you need custom routes or attribute routing.
Custom Route with Multiple Parameters
Route order matters — MVC evaluates routes top to bottom and uses the first match.
Attribute Routing (Recommended)
Attribute routing places the route directly on the action method, making it easier to read and maintain than centralized route tables.
Route Constraints
| Constraint | Example | Matches |
int | {id:int} | 123, not abc |
alpha | {name:alpha} | abc, not 123 |
bool | {flag:bool} | true, false |
min(n) | {id:min(1)} | 1 and above |
max(n) | {id:max(100)} | 100 and below |
range(a,b) | {id:range(1,100)} | 1 to 100 |
length(n) | {code:length(5)} | exactly 5 chars |
Query String Parameters
Parameters not in the route template are automatically bound from the query string:
Query strings are best for optional filters and search parameters where the URL should remain readable.
Mixing Route and Query Parameters
Model Binding with Complex Parameters
Generating URLs with Multiple Parameters
Common Pitfalls
- Route order matters: MVC matches the first route that fits the URL. If the default route
{controller}/{action}/{id}comes before your custom route, it may capture the URL first. Always place more specific routes before generic ones. - Parameter name mismatch: The route template parameter name (
{category}) must match the action method parameter name (string category). A mismatch causes the parameter to be null or trigger a 404. - Missing route constraints: Without constraints,
/Products/Details/abcmatches{id}even if the action expects anint, causing a runtime error. Add{id:int}constraints to reject non-matching URLs early with a 404. - Forgetting to enable attribute routing:
[Route]attributes are ignored unlessroutes.MapMvcAttributeRoutes()is called inRouteConfig.cs. Without it, only convention-based routes work. - Ambiguous routes: Two routes that match the same URL pattern cause an
AmbiguousMatchException. Use constraints or different URL structures to disambiguate.
Summary
- The default route handles one parameter — add custom routes for multiple parameters
- Attribute routing (
[Route]) is more readable than centralizedMapRoutedefinitions - Use route constraints (
{id:int},{name:alpha}) to validate parameters in the URL - Use query strings for optional filters and search parameters
- Model binding automatically maps query string parameters to complex objects
- Place specific routes before generic ones — MVC uses first-match ordering
Related reading
- Run two winform windows simultaneously
- Running Azure functions locally gives No runtime error after .NET7 upgrade
- Running MSIL on GPU
- Save and load MemoryStream to/from a file
- Searching a tree using LINQ
- Select a DictionaryT1, T2 with LINQ
- Select folder dialog WPF
- SELECT FROM X WHERE id IN ... with Dapper ORM

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.