
问题重现:
当项目下:Controller/HomeController.cs时,
运行报错:
Server Error in '/' Application.
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:
Fckeditor.Controllers.HomeController
MvcApplication2.Controllers.HomeController
解决办法:
/Global.asax

代码
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//在Global.asax的MapRoute也加入了这个命名空间
routes.MapRoute(
"Home",
"{controller}/{action}/{id}",
new { Controller="Home",Action="index",id=UrlParameter.Optional},
new string[] { "MvcApplication2.Controllers" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "CreateTools", id = UrlParameter.Optional } // Parameter defaults
);
}
本文解决了一个ASP.NET MVC项目中因存在多个HomeController导致的运行时错误问题。通过在Global.asax文件中注册特定的路由并指定命名空间,成功避免了控制器名称冲突。
1059

被折叠的 条评论
为什么被折叠?



