In this article, we shall learn how to use Area in ASP.NET Core web application.
Introduction
Areas are an ASP.NET MVC feature that is used to modularize the application development so that development and maintenance is easy.
If you are not familiar with ASP.NET MVC Core, please read
Let's Explore ASP.NET Core 1.0 briefly. In this application, we shall learn how to use Areas in ASP.NET Core application.
Background
As against ASP.NET MVC 5, where we had a right-click menus to add Areas and the folder structure and default Controller and Views gets created automatically, ASP.NET Core has everything almost manual. So we have to do all things manually here.
Create Area Folder Structure
In below example, I have shown an Area named "Admin" where I have created all the folder structures manually.
Creating Controller
To create a Controller, say 'HomeController.cs', right click the 'Controllers' folder and go to Add > Class....
'Add New Item' dialog box appears as shown below, now write 'controller' in the search box as displayed above and select 'MVC Controller Classs' template. Write the controller name (must end with word 'Controller') and click on 'Add' button.
You will get a controller created with an Index method. Now add an Area("Admin")]
attribute to the controller class as shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace FirstCoreApplication.Areas.Admin.Controllers
{
[Area("Admin")]
public class HomeController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
public IActionResult Details()
{
return View();
}
}
}
Creating View
In ASP.NET Core it is not that easy to create View like ASP.NET MVC 5, you will have to again right click the View folder (In this case, I want to add view for Home controller, so right click Home folder) and go to Add > New Item...
A dialog box appears like this, now select 'MVC View Page' template, write the view name and click Add.
You will get a blank View page and you will have to write everything from scratch. To add a Layout for this page, set the property as we used to set in ASP.NET MVC.
@*
For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<h1>Admin Home</h1>
@Html.ActionLink("Shown Details", "Details", "Vendors", new { area = "Admin" })
Creating Models
Similar to Controllers and VIews, you can also create Folder for Models manually.
Routing settings for Areas in ASP.NET Core
The most important settings for Area to work in ASP.NET Core is the Configure
method of ~/Startup.cs file. Look at the UserMvc
method and notice the highlighted code.
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
The highlighted code says that we want to set an route for an Area that exists and if controller name is not specified with an Area then it will take 'Home' and Index method is not specified then it will take 'Index' and Id is optional.
URLs generation for Area
To access the area created (In this case, we created Admin
area with Home
controller and Index
action method), we can use any of below url
- http://localhost:58767/Admin/ - Admin area with default controlller and action method
- http://localhost:58767/Admin/Home - Admin area with Home controller
- http://localhost:58767/Admin/Home/Index - Admin area with Home controller and Index action method
To use the Tag helper to create a hyperlink on the page write this
<a asp-action="Details" asp-area="Admin" asp-controller="Vendors">Details</a>
Note: just copy-pasting above code doesn't create a valid url, we need to copy '_ViewImports.cshtml' page from ~/Views/_ViewImports.cshtml into ~/Areas/Admin/Views/_ViewImports.cshtml'. Read more about this here.
Html helper methods for Area can be used like this
@Html.ActionLink("Shown Details", "Details", "Vendors", new { area = "Admin" })
Conclusion
ASP.NET Core has many new features however less friendly for developers [;(], my personal view based on a very little work I have done in ASP.NET Core is that we need to write more code to do simple stuffs when we compare this with ASP.NET MVC 5. However as many says, Performance, Open source, Operating system friendliness and many other features makes ASP.NET Core, the need of an hour.
Reference
Areas