Organizing apps with areas
As an app grows, the Controllers and Views folders get crowded. Areas let you split a large MVC app into sections, such as Admin and Store, each with its own controllers and views. This lesson shows the folder layout, routing and links.
- StartURL arrives/admin/users/edit/3
- Step 2Area route matches{area:exists}/{controller=Home}/{action=Index}/{id?}
- Step 3Controller foundUsersController marked with [Area("Admin")]
- Step 4Action runsEdit(3) loads the user
- EndView foundAreas/Admin/Views/Users/Edit.cshtml
The idea in plain words
An area is a folder that contains its own Controllers, Models and Views, like a mini MVC app inside your app. A shop might have an Admin area for staff pages and keep public pages in the main folders. Each area keeps its files together, and two areas can even have controllers with the same name.
- Areas/Admin/Controllers/UsersController.cs
- Areas/Admin/Views/Users/Edit.cshtml
- Areas/Admin/Views/_ViewStart.cshtml and _ViewImports.cshtml (copy them into each area)
Example: controller and routes
Mark each area controller with [Area]. Add the area route before the default route so area URLs are matched first.
[Area("Admin")]
public class UsersController : Controller
{
public IActionResult Edit(int id) => View();
}
// Program.cs
app.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");Links between areas
Use asp-area in links. An empty value links out of an area back to the main app.
<a asp-area="Admin" asp-controller="Users" asp-action="Edit" asp-route-id="3">Edit user</a>
<a asp-area="" asp-controller="Home" asp-action="Index">Back to the shop</a>Common mistakes
- Forgetting the [Area] attribute, so the controller is treated as part of the main app.
- Adding the area route after the default route, so area URLs never match it.
- Not copying _ViewImports.cshtml into the area, so Tag Helpers stop working in area views.
Interview question
Why would you use areas in an ASP.NET Core MVC application?
Areas organize a large app into functional sections, each with its own controllers, views and models, which keeps folders manageable, lets teams work on sections independently, and allows the same controller name in different areas. They also make it easy to apply settings such as authorization or a different layout to a whole section.
Want the full details? Read the official Microsoft documentation for this topic.