[.NET MVC]Multiple types were found that match the controller named 'XXX'

摘要:[.NET MVC]Multiple types were found that match the controller named 'XXX'

前言

在MVC的架構,當我們要註冊一個routing時我們通常會用以下方法註冊

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

上面我們很簡單的註冊了一個Default的routing給Home這個Controller,但當我們有引用dll的時候好死不好dll裡面也有包入一個同樣名稱的Controller時就會導致.net拋出以下錯誤

controller

How to fix it

解決辦法有兩種,一種是設定單一Routing的Controller Namespace

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new[] {"YourCurrentProjectNamespace.WebSite.Controllers"}
);

另外一種就是把整個site的Routing都設定預設的Controller Namespace

ControllerBuilder.Current.DefaultNamespaces.Add("YourCurrentProjectNamespace.WebSite.Controllers");

以上的設定都在App_Start>RouteConfig.cs中


如果覺得文章還不錯麻煩請在文章最上面給予推薦,你的支持是小弟繼續努力產出的動力!