Reputation: 6291
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!
Upvotes: 7
Views: 2035
Reputation: 1855
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
{ //... }
Upvotes: 5