ASP.NET Core MVC Interview Question #87

How do you configure routing for Areas?

Advanced Routing, Filters, Areas & Application Structure Mid-Level Intermediate

Quick Interview Answer

A conventional area route includes an {area:exists} segment and is normally mapped before the application's general default route. Attribute routing can also include [area] or explicit area templates.

Detailed Explanation

A conventional area route includes an {area:exists} segment and is normally mapped before the application's general default route. Attribute routing can also include [area] or explicit area templates.

The area route value must correspond to an existing MVC area for the area constraint to match.

Code Example

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

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