Search code examples
c#asp.net-mvcasp.net-mvc-routingforms-authenticationuser-accounts

ASP.NET MVC [Authorize] from controller in area can't find Account/Login ActionResult in root folder


I'm trying to setup admin logins for my MVC site. I have an area called Admin with a controller called ControlPanel. I've placed the [Authorize] attribute above the ControlPanel Controller declaration. When I make a call to the index of that controller I get an error saying

it can't find the Account/Login resource.

The AccountController is stored in the root's 'Controllers' folder. The view exists in the root's 'Views' folder.

My AdminAreaRegistration class:

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "{area}/{controller}/{action}/{id}",
            new { area="Admin", action = "Index", id = UrlParameter.Optional }
        );
    }
}

My RouteConfig class:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{area}/{controller}/{action}/{id}",
            defaults: new { area = "", controller = "Startup", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Thanks in advance


Solution

  • Your route seems to be messed up. Try the code below to configure your routing.

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { area="Admin", action = "Index", id = UrlParameter.Optional }
        );
    }
    
    
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {controller = "Startup", action = "Index", id = UrlParameter.Optional }
        );
    }