ASP.NET MVC报错: Multiple types were found that match the controller named
2014-12-15 09:48
731 查看
当使用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 string[] { "MvcTest.Web.Controllers" }
);
}
注意上面的new string[] { "MvcTest.Web.Controllers" }参数,在Area的对应AreaRegistration.cs类中的routes.MapRoute方法中,也加入这个参数,不同的是字符串要改写成Area的Controller对应的命名空间,如new[] { "MvcTest.Web.Areas.TestArea.Controllers" }
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 string[] { "MvcTest.Web.Controllers" }
);
}
注意上面的new string[] { "MvcTest.Web.Controllers" }参数,在Area的对应AreaRegistration.cs类中的routes.MapRoute方法中,也加入这个参数,不同的是字符串要改写成Area的Controller对应的命名空间,如new[] { "MvcTest.Web.Areas.TestArea.Controllers" }
相关文章推荐
- MVC 区域内默认控制器不能访问(Multiple types were found that match the controller named ‘Index')
- 使用Area出现错误:Multiple types were found that match the controller named 'Home'
- Multiple types were found that match the controller
- Multiple actions were found that match the request Web API
- Multiple actions were found that match the request
- ASP.NET MVC 6 attribute routing – the [controller] and [action] tokens
- VS 2012 No exports were found that match the constraint 解决办法
- No exports were found that match the constraint.
- Can we call the Method of a controller from another controller in asp.net MVC?
- 修复 Visual Studio Error “No exports were found that match the constraint”
- 【引】How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- VS 2012 No exports were found that match the constraint 解决办法
- How to get the Current Controller Name, Action, or ID in ASP.NET MVC
- VS 2012 No exports were found that match the constraint 解决办法
- VS2012 No exports were found that match the constraint
- Error in Microsoft Visual Studio 2012 :No exports were found that match the constraint
- ASP.Net MVC Entity Framework Error – More than one context type was found in the assembly ‘ProjectNa
- How to Choose the Best Way to Pass Multiple Models in ASP.NET MVC
- HTML5, jQuery Mobile and ASP.NET MVC 4 – Using the ViewModel between the model and controller
- No exports were found that match the constraint.