使用Area出现错误:Multiple types were found that match the controller named 'Home'
[已解决] 使用Area出现错误:Multiple types were found that match the controller named 'Home'
当前页面:http://www.senparc.com/SZD-107
{ 收藏当前页面 }
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
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
最佳答案
这是因为你定义了两个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" }
只需要在你的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
楼上的ntgjh可以去这里提问:http://www.senparc.com/SZD.xhtml/NewQuestion
在这里已经回答过这个主题的人,就没法回答你了。
另外最好把错误信息描述的具体一点,或者复制上来,更有利于别人了解你的情况,从而得到最佳答案:)
===================
此回答修改于:2010-5-29 23:07:07
回答时间:2010/5/29 23:07:07
| 回答者:GaLiJiKuai
提交失败!请检查错误!错误信息:
以下信息或许对您有用:
- [已解决] 20 ASP.NET MVC 3.0自定义ViewEngine后出现错误:must d... 2012/8/25 22:02:58
- [已解决] 100 ASP.NET MVC 3 RC2下载地址?有说明文档的话更好!谢谢! 2010/12/12 12:23:00
- [已解决] 20 The type 'System.Data.Objects.DataClasse... 2010/11/25 16:51:58
- [已解决] 20 ASP.NET MVC 3 RC中的Razor语法,在VS中已经实现了智能感知和... 2010/11/14 15:51:22
- [已解决] 60 ASP.NET MVC Razor 在VS2010中智能感知的插件哪里有下载? 2010/11/3 12:53:18