8.8 — Areas¶
← Roadmap · Section: 8. Routing Prev: 8.7 Endpoint precedence · Next: 8.9 Route groups
Markers: — · Confidence: 🔴 · Last reviewed: —
TL;DR¶
An area is a way to split a large MVC application into sub-applications, each with its own Controllers, Views and Models folders.
- Mainly used to separate something like an Admin section from the public site.
- Requires a matching folder structure, an
[Area]attribute, and a route pattern containing{area:exists}. - ⚠️ Rarely the right tool today. For APIs use route prefixes; for a large app, separate projects or a modular monolith.
Definitions¶
| Term | What it is | What it means |
|---|---|---|
| Area | an MVC organisational feature | A named sub-application with its own controllers and views. |
[Area("Name")] |
an attribute | Marks a controller as belonging to an area. |
{area:exists} |
a route constraint | Matches only if the segment names a registered area. |
Areas/ folder |
a convention | Areas/{AreaName}/Controllers, /Views, /Models. The structure is required. |
MapAreaControllerRoute |
a method | Registers a route scoped to one specific area. |
asp-area |
a tag helper attribute | Specifies the area when generating a link in a Razor view. |
| View discovery | how Razor finds a view | Areas add extra search paths under Areas/{Area}/Views. |
| Modular monolith | an architecture | One deployable app split into independent modules. The modern alternative. |
The concept¶
What problem areas solved¶
A large MVC site accumulates controllers. Twenty for the public site, fifteen for the admin
panel, ten for a partner portal — all sitting in one flat Controllers folder, with the views
equally mixed in one Views folder.
Areas let you split that:
Controllers/ ← the public site
Views/
Areas/
├── Admin/
│ ├── Controllers/
│ ├── Views/
│ └── Models/
└── Partner/
├── Controllers/
├── Views/
└── Models/
Each area is effectively its own mini-application with its own URL prefix — /Admin/Users/Index
routes to Areas/Admin/Controllers/UsersController.Index.
A picture: a department store with separate floors. Menswear and homeware each have their own layout and staff, but it is one building with one entrance and one till system.
⚠️ Why you probably should not use them¶
Areas date from ASP.NET MVC in 2010, when a single large web application was the normal shape. That is much less common now.
For an API, areas are the wrong tool. A route prefix does the same job with none of the folder requirements:
[Route("api/admin/[controller]")] // ✓ simple, no area machinery
public class UsersController : ControllerBase { }
For a genuinely large application, the better answers are separate projects, a modular monolith, or route groups for minimal APIs (8.9).
Areas remain reasonable for one case: a server-rendered MVC or Razor Pages site with a distinct admin section that shares the same deployment and authentication. If that is not you, skip them.
So the honest reason to know this topic is recognising it in existing codebases, and being able to say why you would not add it to a new one.
How it works¶
The simple version — the three requirements¶
All three are needed, and missing any one produces a confusing failure.
1. The folder structure:
Areas/
└── Admin/
├── Controllers/
│ └── UsersController.cs
├── Views/
│ ├── Users/
│ │ └── Index.cshtml
│ └── _ViewStart.cshtml
└── Models/
2. The [Area] attribute:
[Area("Admin")]
public class UsersController : Controller
{
public IActionResult Index() => View();
}
3. A route pattern with {area:exists}:
app.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
⚠️ The area route must be registered first. Conventional routes are matched in registration order (8.7), and the default pattern matches almost anything — so registering it first means area URLs never reach the area route.
⚠️ :exists is doing real work. It restricts the segment to names that are actually
registered areas, so /Products/Details/5 is not misread as area Products, controller
Details, action 5. Without it, every two-or-three-segment URL becomes ambiguous.
Attribute routing with areas¶
[Area("Admin")]
[Route("admin/[controller]")] // explicit prefix
public class UsersController : Controller
{
[HttpGet]
public IActionResult Index() => View();
}
Or using the token:
[Area("Admin")]
[Route("[area]/[controller]/[action]")] // → /Admin/Users/Index
public class UsersController : Controller { }
⚠️ With attribute routing, [Area] still matters even though the URL is explicit, because
view discovery depends on it. Without the attribute, Razor looks in Views/Users/ rather than
Areas/Admin/Views/Users/ and you get a "view not found" error listing the paths it searched.
That error message is at least helpful — it tells you exactly where it looked.
How it actually works — view discovery¶
Areas change where Razor searches for a view:
Areas/{Area}/Views/{Controller}/{Action}.cshtml
Areas/{Area}/Views/Shared/{Action}.cshtml
Views/Shared/{Action}.cshtml ← the global fallback
So shared layouts still work. An area's views can use Views/Shared/_Layout.cshtml without
duplicating it, while area-specific partials live in Areas/Admin/Views/Shared/.
Each area usually gets its own _ViewStart.cshtml and _ViewImports.cshtml, which is how an
admin section applies a different layout (9.4).
Generating links across areas¶
⚠️ This is the part that trips people up. IUrlHelper reuses ambient route values
(8.6), and area is one of them:
// Currently in the Admin area
@Html.ActionLink("Users", "Index", "Users")
// → /Admin/Users — the area was inherited, which is usually what you want
But linking out of an area requires clearing it explicitly:
// ✗ From inside Admin, this still generates /Admin/Home/Index
@Html.ActionLink("Public site", "Index", "Home")
// ✓ Clear the area with an empty string
<a asp-area="" asp-controller="Home" asp-action="Index">Public site</a>
An empty string clears it; omitting it inherits. That asymmetry is genuinely confusing and is the single most common area-related bug.
Linking into an area from outside:
Razor Pages areas¶
Razor Pages supports areas too, with a different folder convention:
builder.Services.AddRazorPages(options =>
{
options.Conventions.AuthorizeAreaFolder("Admin", "/", policy: "AdminOnly");
});
That AuthorizeAreaFolder line is genuinely useful — it applies an authorization policy to every
page in an area with one call, rather than an attribute per page
(16.8).
The deeper detail — securing an area¶
The common reason for an Admin area is that it needs different authorization. Applying that per-controller is repetitive and easy to forget on a new controller:
// ✗ Repeated on every admin controller, and forgotten on the next one
[Area("Admin")]
[Authorize(Policy = "AdminOnly")]
public class UsersController : Controller { }
A convention applies it to the whole area at once:
builder.Services.AddControllersWithViews(options =>
{
options.Conventions.Add(new AreaAuthorizationConvention("Admin", "AdminOnly"));
});
public class AreaAuthorizationConvention(string area, string policy) : IControllerModelConvention
{
public void Apply(ControllerModel controller)
{
var controllerArea = controller.Attributes
.OfType<AreaAttribute>()
.FirstOrDefault()?.RouteValue;
if (string.Equals(controllerArea, area, StringComparison.OrdinalIgnoreCase))
controller.Filters.Add(new AuthorizeFilter(policy));
}
}
That is the pattern worth knowing. A new controller added to the area is protected automatically, which removes the "someone forgot the attribute" failure mode — and that failure mode is a genuine security hole, not just an inconvenience.
Code¶
A complete area setup:
MyApp/
├── Controllers/
│ └── HomeController.cs
├── Views/
│ ├── Home/Index.cshtml
│ └── Shared/_Layout.cshtml ← shared with areas
├── Areas/
│ └── Admin/
│ ├── Controllers/
│ │ ├── UsersController.cs
│ │ └── SettingsController.cs
│ └── Views/
│ ├── Users/Index.cshtml
│ ├── Shared/_Layout.cshtml ← admin-specific layout
│ ├── _ViewStart.cshtml
│ └── _ViewImports.cshtml
└── Program.cs
// Program.cs — area route FIRST
app.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// Areas/Admin/Controllers/UsersController.cs
[Area("Admin")]
[Authorize(Policy = "AdminOnly")]
public class UsersController(IUserService users) : Controller
{
public async Task<IActionResult> Index(CancellationToken ct)
=> View(await users.ListAsync(ct));
}
<!-- Navigation, showing both directions -->
<nav>
<!-- Into the area -->
<a asp-area="Admin" asp-controller="Users" asp-action="Index">Admin</a>
<!-- Out of the area — the empty string is ESSENTIAL -->
<a asp-area="" asp-controller="Home" asp-action="Index">Public site</a>
</nav>
The modern alternative¶
For anything new, this achieves the same separation with none of the machinery:
// An API — just a route prefix
[Route("api/admin/[controller]")]
[Authorize(Policy = "AdminOnly")]
public class UsersController : ControllerBase { }
// Minimal APIs — a route group, with the policy applied once. See 8.9.
var admin = app.MapGroup("/api/admin").RequireAuthorization("AdminOnly");
admin.MapGet("/users", ListUsers);
admin.MapPost("/users", CreateUser);
The route group version does everything an area does for an API — a URL prefix, shared authorization, logical grouping — in three lines with no folder conventions.
Interview questions¶
Q: What are areas and what problem do they solve?
Areas let you split a large MVC application into named sub-applications, each with its own
Controllers, Views and Models folders under Areas/{Name}. The typical use is separating an admin
section from a public site so the controllers and views are not all mixed together in one flat
folder. Setting one up needs three things that must agree: the folder structure, an [Area]
attribute on the controller, and a route pattern containing {area:exists} registered before the
default pattern.
Q: Would you use areas in a new application?
Probably not. They date from 2010, when one large web application was the normal shape. For an API a route prefix achieves the same separation with none of the folder conventions, and for minimal APIs a route group also gives you shared authorization in the same call. For a genuinely large application, separate projects or a modular monolith are better answers. Areas remain reasonable for a server-rendered MVC site with a distinct admin section sharing one deployment — mostly you need to know them to work in existing codebases.
Q: What does {area:exists} do?
It constrains that segment to names that are actually registered as areas, rather than matching
anything. Without it, a URL like /Products/Details/5 would be ambiguous — it could be area
Products, controller Details, action 5, or the ordinary controller/action/id shape. The
constraint means the area route only matches real areas, so everything else falls through to the
default pattern. The area route also has to be registered first, because conventional routes match
in registration order and the default pattern matches almost everything.
Q: What is the common bug when generating links with areas?
Forgetting that area is an ambient route value. Inside an area, a link that does not mention the
area inherits the current one — so a link to the public home page generated from inside Admin
produces /Admin/Home/Index rather than /Home/Index. You have to clear it explicitly with
asp-area="", an empty string. Omitting the attribute inherits and an empty string clears, which
is an asymmetry people find genuinely confusing.
Traps ⚠️¶
- The area route must be registered before the default route, or area URLs never match.
- All three pieces must agree — folder,
[Area]attribute, route pattern. Missing one gives a confusing "view not found" or a 404. asp-area=""clears the area; omitting it inherits. The most common area bug.[Area]matters even with attribute routing, because view discovery depends on it.- Without
{area:exists}, ordinary URLs are misread as area URLs. - Forgetting
[Authorize]on a new area controller is a security hole. Use a convention instead. - Areas do not apply to minimal APIs — use route groups.
- Each area usually needs its own
_ViewStartand_ViewImports, or it inherits the root layout.
Related¶
- 8.3 Conventional vs attribute routing — registration order
- 8.6 Link generation — ambient values and
[area] - 8.7 Endpoint precedence — why order matters for conventional routes
- 8.9 Route groups — the modern alternative
- 9.4 Razor layouts —
_ViewStartper area - 16.8 Authorization everywhere — securing a whole area
- 4.11 Project structure — feature folders instead