Search code examples
asp.net-mvc-5url-routingasp.net-mvc-routing

asp.net MVC default action not working in area


I have a project with a setup area declared and a URL that works as long as I explicitly include the "/Index" action. The problem is when I use RedirectToAction("Index") it excludes "/Index" from the URL and I get a 404 Page Not Found. The URLs are:

http://localhost:40078/Setup/Facility/Index - Works
http://localhost:40078/Setup/Facility/ - 404 (with or without trailing /)

RouteConfig:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapMvcAttributeRoutes();

        AreaRegistration.RegisterAllAreas();

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "T.C.MVCWeb.Controllers" }
        );
    }

SetupAreaRegistration:

public class SetupAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Setup";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Setup_default",
            "Setup/{controller}/{action}/{id}",
            defaults: new { action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "T.C.MVCWeb.Areas.Setup.Controllers" }
        );
    }
}

Requested Controller

namespace T.C.MVCWeb.Areas.Setup.Controllers
{
    public class FacilityController : Controller
    {
        // GET: FacilitySetup
        public ActionResult Index()
        {
            ...
        }
    }
}

I added Phil Haack's RouteDebugger and can see that the 404 request thinks "Setup" is the controller, but I don't understand why and the rest of the output is confusing me more than it's helping.


Solution

  • Thanks to NightOwn888's comment, I found a route declared via an attribute. The offending route attribute declared Setup/{action} which was intended to allow the area URL itself http://localhost:40078/Setup/ to map to a SetupController.