← Back to Articles         
MapControllerRoute() vs MapControllers()

MapControllerRoute() vs MapControllers()

Published on 27 Sep 2026     2 min read ASP.NET Core MVC
MVC

MapControllerRoute() vs MapControllers()

Feature MapControllerRoute() MapControllers()
Routing type Conventional routing Attribute routing
Route definition Defined centrally in Program.cs Defined with attributes on controllers/actions
Common usage MVC applications with Razor Views Web APIs and custom MVC routes
Route pattern Requires a route pattern Does not require a central pattern
Example attribute Not required [Route], [HttpGet], [HttpPost]
URL structure Usually follows controller/action pattern Fully controlled through attributes

MapControllerRoute() Example

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
/Products/Details/10

It maps to:

ProductsController.Details(10)

MapControllers() Example

app.MapControllers();
[Route("products")]
public class ProductsController : Controller
{
    [HttpGet("details/{id:int}")]
    public IActionResult Details(int id)
    {
        return View();
    }
}
/products/details/10

Using Both

Both routing styles can be used in the same application:

app.MapControllers();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

Key Points

  • Use MapControllerRoute() for conventional MVC routing.
  • Use MapControllers() for attribute-routed controllers.
  • MapControllers() does not automatically create a default MVC route.
  • Use both when the application requires conventional and attribute routing.

 

Route Parameters, Optional Parameters and Constraints

Type Purpose Syntax Example URL
Route parameter Captures a required URL value {id} /products/details/10
Optional parameter Value is not mandatory {id?} /products/details
Route constraint Restricts the accepted value type or format {id:int} /products/details/10

1. Route Parameters

A route parameter captures a value from the URL and passes it to the action.

[HttpGet("products/details/{id}")]
public IActionResult Details(int id)
{
    return View();
}
/products/details/10

Here, id receives 10.

2. Optional Parameters

Add ? to make a route parameter optional.

[HttpGet("products/details/{id?}")]
public IActionResult Details(int? id)
{
    return View();
}

Both URLs are valid:

/products/details
/products/details/10

Use a nullable type or default value when the parameter is optional:

public IActionResult Details(int id = 0)

3. Route Constraints

Constraints allow a route to match only specific values.

[HttpGet("products/details/{id:int}")]
public IActionResult Details(int id)
{
    return View();
}

Valid:

/products/details/10

Not matched:

/products/details/laptop

Common Constraints

Constraint Purpose Example
int Integer value {id:int}
long Long integer {id:long}
bool Boolean value {active:bool}
datetime Date value {date:datetime}
guid GUID value {id:guid}
min Minimum numeric value {id:min(1)}
max Maximum numeric value {id:max(100)}
length Required string length {code:length(5)}
alpha Alphabetic characters {name:alpha}

Constraints can be combined:

[HttpGet("products/{id:int:min(1)}")]

Conventional Route Example

app.MapControllerRoute(
    name: "product",
    pattern: "products/{action=Index}/{id:int?}",
    defaults: new { controller = "Products" });

Key Points

  • Route parameters capture values from the URL.
  • Optional parameters use ?.
  • Constraints control whether a route matches; they are not input validation.
  • Parameter names should match the action parameter names.
  • A route that does not match usually produces 404 Not Found.

 

← Back to Articles