(转)找到多个与名为“Home”的控制器匹配的类型。解决方法

“/”应用程序中的服务器错误。


找到多个与名为“Home”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。如果是这样,请通过调用带有 'namespaces' 参数的 "MapRoute" 方法的重载来注册此路由。

“Home”请求找到下列匹配的控制器:
WebAppAreasDemo.Controllers.HomeController
WebAppAreasDemo.Areas.PharmaceuticalCompanies.Controllers.HomeController

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 找到多个与名为“Home”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。如果是这样,请通过调用带有 'namespaces' 参数的 "MapRoute" 方法的重载来注册此路由。

“Home”请求找到下列匹配的控制器:
WebAppAreasDemo.Controllers.HomeController
WebAppAreasDemo.Areas.PharmaceuticalCompanies.Controllers.HomeController

源错误:

执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。


堆栈跟踪:

[InvalidOperationException: 找到多个与名为“Home”的控制器匹配的类型。如果为此请求(“{controller}/{action}/{id}”)提供服务的路由没有指定命名空间以搜索与此请求相匹配的控制器,则会发生这种情况。如果是这样,请通过调用带有 'namespaces' 参数的 "MapRoute" 方法的重载来注册此路由。

“Home”请求找到下列匹配的控制器:
WebAppAreasDemo.Controllers.HomeController
WebAppAreasDemo.Areas.PharmaceuticalCompanies.Controllers.HomeController]
   System.Web.Mvc.DefaultControllerFactory.GetControllerTypeWithinNamespaces(RouteBase route, String controllerName, HashSet`1 namespaces) +159
   System.Web.Mvc.DefaultControllerFactory.GetControllerType(RequestContext requestContext, String controllerName) +544
   System.Web.Mvc.DefaultControllerFactory.System.Web.Mvc.IControllerFactory.GetControllerSessionBehavior(RequestContext requestContext, String controllerName) +53
   System.Web.Mvc.MvcRouteHandler.GetSessionStateBehavior(RequestContext requestContext) +132
   System.Web.Mvc.MvcRouteHandler.GetHttpHandler(RequestContext requestContext) +33
   System.Web.Mvc.MvcRouteHandler.System.Web.Routing.IRouteHandler.GetHttpHandler(RequestContext requestContext) +10
   System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) +9843503
   System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) +82
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69

 


版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.6.1055.0




解决方法:


RouteConfig.cs注册路由添加命名空间(namespaces)参数

复制代码
namespace WebAppAreasDemo
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "WebAppAreasDemo.Controllers" }
            );
        }
    }
}
复制代码

 

现在访问http://localhost:2353/正常了,然而只输入区域名称访问http://localhost:2353/PharmaceuticalCompanies/,提示如下:

“/”应用程序中的服务器错误。


无法找到资源。

说明: HTTP 404。您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。

请求的 URL: /PharmaceuticalCompanies/


版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.6.1055.0


这又是闹什么鬼,看下区域下的PharmaceuticalCompaniesAreaRegistration.cs注册类,发现没有设置默认的控制器

复制代码
namespace WebAppAreasDemo.Areas.PharmaceuticalCompanies
{
    public class PharmaceuticalCompaniesAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "PharmaceuticalCompanies";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.MapRoute(
                "PharmaceuticalCompanies_default",
                "PharmaceuticalCompanies/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}
复制代码

修改

new { action = "Index", id = UrlParameter.Optional }

添加默认的控制器名称

new { controller="Home", action = "Index", id = UrlParameter.Optional }


现在再只输入区域名称访问http://localhost:2353/PharmaceuticalCompanies/,终于正常了。

posted @ 2019-08-27 14:51  凡的世界  阅读(483)  评论(0编辑  收藏  举报