Having issue with multiple controllers of the same name in my project

I am running into the following error with my ASP.NET MVC 3 project:

Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('Home/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'Home' has found the following matching controllers: MyCompany.MyProject.WebMvc.Controllers.HomeController MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController

I have a HomeController in my default controller folder, with a class name of MyCompany.MyProject.WebMvc.Controllers.HomeController.

My RegisterRoutes method, in my global.asax, looks like:

    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
        );
    }

I then have an area called Company, with a HomeController in the default controller folder for the area, with a class name of MyCompany.MyProject.WebMvc.Areas.Company.Controllers.HomeController.

The RegisterArea method in the CompanyAreaRegistration file looks like:

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

This is all leading the error I highlighted at the beginning of this post. I am struggling trying to piece together a solution from various other posts, with NO LUCK .

Is it possible to have a HomeController in the default controllers folder and then one in EACH area? If so, do I need to make (assuming I do) changes to my configuration file to make this work?

Any help would be much appreciated!


The error message contains the recommended solution: "If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter."

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

This will make http://server/ go to your HomeController's Index action which is, I think, what you want. http://server/company/home will go to the Company area's HomeController's Index action, as defined in the area registration.


这是asp.net mvc4的方法:

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

我已经重命名了命名空间,所以,我只删除了 de文件夹binobj重建 ,再次运行。

链接地址: http://www.djcxy.com/p/80086.html

上一篇: 你如何定义ASP.Net MVC 4中特定区域的布局?

下一篇: 在我的项目中遇到同名多个控制器的问题