Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Areas in ASP.NET 5

I have an ASP.NET vNext (5) project. I am trying to add two areas to the project. My question is, how do I register areas in vNext? The System.Web.Mvc namespace is gone, which is where AreaRegistrationContext was located. I started looking in the MVC source code on GitHub. I found the Area attribute. However, I'm not sure how to utilize it now.

Can someone please explain to me (or provide a link) of how to use Areas in ASP.NET vNext?

Thank you!

like image 970
JQuery Mobile Avatar asked Dec 02 '14 16:12

JQuery Mobile


1 Answers

In vNext you register and configure the services you are going to use in Startup.cs. Area routes are added like normal routes. There is a sample here: https://github.com/aspnet/Mvc/blob/a420af67b72e470b9481d6b2eca29f7c7c2254d2/samples/MvcSample.Web/Startup.cs

You could add an MVC route for an area like this:

app.UseMvc(routes =>
{
   routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");
});

Or you could use a route attribute like this: [Route("[area]/Home")]

The [Area] attribute decorates controllers included in the area. It takes only one parameter, the name of the area. Here's an example: https://github.com/aspnet/Mvc/blob/a420af67b72e470b9481d6b2eca29f7c7c2254d2/samples/MvcSample.Web/Areas/Travel/Controllers/HomeController.cs

[Area("Travel")]
public class HomeController : Controller
{  //... }
like image 93
Walter Avatar answered Sep 16 '22 22:09

Walter