使用Area出现错误:Multiple types were found that match the controller named 'Home'

[已解决] 使用Area出现错误:Multiple types were found that match the controller named 'Home'

20
[Web开发 > ASP.NET > ASP.NET MVC]
当使用ASP.NET MVC2.0的Area功能时,出现了这样的错误:

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:
MvcTest.Web.Controllers.HomeController
MvcTest.Web.Areas.TestArea.Controllers.HomeController
提问时间:2010/5/15 19:18:58 | 提问者:Souidea | 悬赏:20 | 浏览:1229
最佳答案
这是因为你定义了两个HomeController(默认的一个,Area中一个),系统无法确定你默认的Controller=“Home”到底是哪一个。

只需要在你的global.asax中的routes.MapRoute()方法中,添加一个默认的命名空间,如:

       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 = "" },  // Parameter defaults
               new[] { "MvcTest.Web.Controllers" }
           );

       }

注意上面的new[] { "MvcTest.Web.Controllers" }参数,在Area的对应AreaRegistration.cs类中的routes.MapRoute方法中,也加入这个参数,不同的是字符串要改写成Area的Controller对应的命名空间,如new[] { "MvcTest.Web.Areas.TestArea.Controllers" }
回答时间:2010/5/15 19:53:17 | 回答者:zsu
其他参考答案(2)
添加命名空间后,的确能执行相应的action,但却提示找不到对应的view,仍提示从根目录中寻找,并没有在TestArea的view中寻找,如何解决?
回答时间:2010/5/29 10:14:06 | 回答者:ntgjh
@ntgjh:
楼上的ntgjh可以去这里提问:http://www.senparc.com/SZD.xhtml/NewQuestion
在这里已经回答过这个主题的人,就没法回答你了。

另外最好把错误信息描述的具体一点,或者复制上来,更有利于别人了解你的情况,从而得到最佳答案:)

===================
此回答修改于:2010-5-29 23:07:07
回答时间:2010/5/29 23:07:07 | 回答者:GaLiJiKuai
提交失败!请检查错误!错误信息:

注:以上所有信息由网友提供,仅供交流、参考,均不代表盛派网络言论,如果有任何问题或不妥,请立即联系我们

以下信息或许对您有用: