0

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
}

1 Answer 1

2

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 }
    );
Sign up to request clarification or add additional context in comments.

3 Comments

MAGIC, Can you explain why this fixed the problem?
MySite/Admin/User/1/Account is also a route of type Admin/{controller}/{action}/{id}
dont forget to mark as answer if this fixed your problem.. this may help others coming to this site

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.