找到多個與名為“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/,終於正常了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM