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”.
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.
1 2 3 4 5 6 |
// 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
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”.
Add an Area tag to the class to specify which area to look for the View under.
1 2 3 4 5 6 7 8 |
[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.