Question

The problem I'm facing is that I have 2 controllers with the same name. One in the main controller folder, and the other in the controller folder in my Admin Area.

Calling the action result directly works fine:

MySite/Admin/Account/GetAccount?userId=1

Calling through the route doesn't work

MySite/Admin/User/1/Account

Any idea What I'm doing wrong?

Application_Start

AreaRegistration.RegisterAllAreas()

RouteConfig

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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
        new[] { "MyCompany.Controllers" }
    );
}

AdminAreaRegistration

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

    context.MapRoute(
        "GetUserAccount",
        "Admin/User/{userId}/Account",
        new { controller = "Account", action = "GetAccount" },
        new[] { "MyCompany.Areas.Admin.Controllers" }
    );
}

My Action Result In Areas/Admin/AccountController

public ActionResult GetAccount(string userId)
{
    // return Account Type
}
Was it helpful?

Solution

i think you should change the positions of the account and check again

 context.MapRoute(
        "GetUserAccount",
        "Admin/User/{userId}/Account",
        new { controller = "Account", action = "GetAccount" },
        new[] { "MyCompany.Areas.Admin.Controllers" }
    );
context.MapRoute(
        "Admin_default",
        "Admin/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top