How to define Attribute Routing for Area in ASP.NET MVC?

You can also define attribute routing for a controller that belongs to an area by using the RouteArea attribute. When you define attribute routing for all controllers with in an area, you can safely remove the AreaRegistration class for that area.

[RouteArea("Admin")]
[RoutePrefix("menu")]
[Route("{action}")]
public class MenuController : Controller
{
    // route: /admin/menu/login
    public ActionResult Login()
    {
        return View();
    }
    // route: /admin/menu/products
    [Route("products")]
    public ActionResult GetProducts()
    {
        return View();
    }
    // route: /categories
    [Route("~/categories")]
    public ActionResult Categories()
    {
        return View();
    }
}

 

Tagged , . Bookmark the permalink.

Leave a Reply