Jonathan
Jonathan

Reputation: 32868

Do controller names have to be unique even when in separate areas?

I have an ASP.NET MVC website and an area project called 'Admin'.

So far the routing works fine, except that it seems I can't have 2 controllers with the same name in each project.

I thought the following URLs would both work fine:

http://website.com/Home/Index

http://website.com/Admin/Home/Index

But it turns out that I get the following error when accessing either:

The controller name 'Home' is ambiguous between the following types:

MyProject.Website.Controllers.HomeController

MyProject.Admin.Controllers. HomeController

Is this normal, or is there something wrong with my setup?


BTW, here's my routing code:

Main project:

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

    Admin.Routes.RegisterRoutes(routes);

    routes.MapAreaRoute(
        "Main",
        "default_route",
        "{controller}/{action}/{URLName}",
        new { controller = "Home", action = "Index", URLName = "" },
        new string[] { "MyProject.Website" }
    );
}

Area project:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.MapAreaRoute(
        "Admin",
        "Admin_Default",
        "Admin/{controller}/{action}",
        new { controller = "Home", action = "Index" },
        new string[] { "MyProject.Admin" }
    );
}

Upvotes: 2

Views: 496

Answers (1)

Jonathan
Jonathan

Reputation: 32868

Found the problem after reading this post.

I should have specified the 'Controllers' namespace in the 5th argument, rather than just the project namespace.

E.g.

new string[] { "MyProject.Admin.Controllers" }

Upvotes: 5

Related Questions