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

当使用ASP.NET MVC的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

---

这是因为你定义了两个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" }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值