找到了多个匹配名为“Home”的控制器的类型。 (两个区域,相同的控制器名称)

时间:2012-01-21 21:36:24

标签: asp.net asp.net-mvc asp.net-mvc-3

这可能与许多人重复,但其中明显的答案并不能解决我的问题。

我明白了:

Multiple types were found that match the controller named 'Home'. This can happen if the route that services this request ('{controller}/{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:
App.Web.Controllers.HomeController
App.Web.Areas.Mobile.Controllers.HomeController

我在Global.ascx.cs中设置了HomeController的默认命名空间:

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

(已验证App.Web.Controllers.HomeController不是拼写错误)。

并在MobileAreaRegistration中注册了Mobile的HomeController:

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

因此,为什么我仍然会看到错误消息?我已经建立/清理并再次运行。结果仍然相同。

这是我注册路线的方式:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

1 个答案:

答案 0 :(得分:14)

在您的Global.asax路线注册中有明显原因替换:

new string[] { "App.Web.Controllers.HomeController" } 

使用:

new string[] { "App.Web.Controllers" } 

这是你应该在那里使用的命名空间约束,而不是特定的类型。