← Back to articles

ASP.Net 5 Areas

Visual Studio 2015 has no GUI support for Areas in ASP.Net 5 yet. The workaround is simple: create the folder structure manually and add an area route in Startup.cs — shown here.

Tedd

Visual Studio 2015 with ASP.Net 5 doesn’t support Areas in the GUI editor. Hopefully that will come, but until then here is a simple workaround.

Create folder structure

In the root of your project (not wwwroot) create the folder structure for Areas manually. In this example I created an area named “Foobar”.

image

Edit Startup.cs to add routes for areas

Change routes under “Configure” in Startup.cs. Inside app.UseMvc() you need to include a route for areas.

// Area route
routes.MapRoute(
 name: "areaRoute", 
 template: "{area:exists}/{controller}/{action}", 
 defaults: new { controller = "Home", action = "Index" }
); 

The whole section will look something like this:

app.UseMvc(routes => {
 // Area route 
 routes.MapRoute(
 name: "areaRoute", 
 template: "{area:exists}/{controller}/{action}", 
 defaults: new { controller = "Home", action = "Index" }
 ); 

 // Default route 
 routes.MapRoute(
 name: "default", 
 template: "{controller=Home}/{action=Index}/{id?}"); 
}); 

Create Controller and specify Area

Create a controller named “HomeController.cs” under “Controllers”.

image

Add an Area tag to the class to specify which area to look for the View under.

[Area("Foobar")]
public class HomeController : Controller { 
 [Route("Index")] 
 [HttpGet] 
 public IActionResult Index() { 
 return View(); 
 } 
} 

Create view

The view is then placed under Views in a sub-folder corresponding to the controllers name.

image

Written by Tedd · Oslo, Norway

Updated 5 Jan 2017

Follow the journal via RSS