Search code examples
asp.net-mvcasp.net-mvc-5url-routingarea

Custom route inside Area's controller action is not working in ASP.NET MVC


I'm using ASP.NET MVC. I'm using custom routes on some of my Actions.

Example:

/Home/Contact

This default route has a custom route defined like this:

[Route("~/contact-us")]
public ActionResult Contact()
{
    // Some code.. :)    
}

And now I can access the Contact Action through the /contact-us route.

Unfortunately, this doesn't work when I try to do the same inside an Area. When I add a custom route in an action inside a controller in an Area and I try to access it, the action is not executed and I'm redirected to my global Home Controller's Index action. Why is this happening and how can I fix it?


Solution

  • May be you should use RouteArea attribute to your controller in Area. Following code snippet may help you.

     [RouteArea("YourArea")]  //use your Area name 
     public class ProfileController : Controller
     {
        [Route("~/contact-us")]
        public ActionResult Contact()
        {
            return View();
        }
     }