在我的项目中有多个同名控制器的问题

我的 ASP.NET MVC 3项目遇到了以下错误:

找到了多种匹配的类型 一个名为“ Home”的控制器。这个可以 如果提供此服务的路线 Request (‘ Home/{ action }/{ id }’)执行 不指定命名空间来搜索 匹配请求的控制器。 如果是这样的话,注册这个 通过调用 方法,该方法接受一个 “名称空间”参数。

对「主页」的要求已找到 下列匹配控制器: 控制器 地区。公司。控制器。家庭控制器

在我的默认控制器文件夹中有一个 HomeController,类名为 MyCompany.MyProject.WebMvc.Controllers. HomeController。

我的 RegisterRoutes 方法在 global.asax 中看起来像:

    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 = UrlParameter.Optional } // Parameter defaults
);
}

然后有一个名为 Company 的区域,该区域的默认控制器文件夹中有一个 HomeController,类名为 MyCompany.MyProject。WebMvc.地区。公司。控制器。家庭控制器。

CompanyArea 注册文件中的 RegisterArea 方法如下:

   public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Company_default",
"Company/{controller}/{action}/{id}",
new { area = "Company", action = "Index", id = UrlParameter.Optional }
);
}

这些都导致了我在本文开头强调的错误。我正在努力拼凑一个解决方案从各种其他职位,与 运气不好

是否有可能在默认控制器文件夹中有一个 HomeController,然后在每个区域中有一个 HomeController?如果是这样,我是否需要(假设我需要)对配置文件进行更改以使其正常工作?

任何帮助都将不胜感激!

92727 次浏览

The error message contains the recommended solution: "If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter."

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "MyCompany.MyProject.WebMvc.Controllers"}
);

This will make http://server/ go to your HomeController's Index action which is, I think, what you want. http://server/company/home will go to the Company area's HomeController's Index action, as defined in the area registration.

This is the asp.net mvc4 approach:

 routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "RegisterNow", id = UrlParameter.Optional },
namespaces: new[] { "YourCompany.Controllers" }
);

Another plausible cause of this issue could be found below:

Multiple types were found that match the controller named 'Home'

Use only the name of the project:

Public Class RouteConfig
Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.MapRoute( _
name:="Default", _
url:="{controller}/{action}/{id}", _
defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional} _
, namespaces:={"MvcAreas"})
End Sub

use this

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

If you're using RazorGenerator, just informing the namespaces parameter could be not enough.

I got to solve adding the statement marked below at Global.asax.cs:

    protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ControllerBuilder.Current.DefaultNamespaces.Add("MyProject.Controllers"); // This one
}

I had renamed the namespaces, so, i only delete de folders bin and obj and rebuild, work again.

As Chris Moschini mention the namespaces parameter may not be enough if you have two areas with same controller name with different namespaces and the default none area route will return 500 server error.

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "MyCompany.MyProject.WebMvc.Controllers"}
);

It "best" to override the default route handler and add this line:

RequestContext.RouteData.DataTokens["UseNamespaceFallback"] = false;

I had this issue after I added a reference to another project that had the same routes and the problem continued after I removed the reference.

Resolved by deleting the .dll file of that added reference from the bin folder and rebuilding.

Like many others, I had this problem after creating a new MVC template project from VS2017 menu, on building the project I would get the op's error message. I then used the answer https://stackoverflow.com/a/15651619/2417292 posted earlier in this thread by cooloverride for mvc4 projects. This still didn't fix my issue so I renamed my Home view folder and HomeController file to be the view folder Company/ and controller file CompanyController. This then worked for me, not a fix per say but a workaround if your not stuck on having the route Home/Index my other issue was I couldn't find the reference causing the error and due to my dev platform being Azure WebApplication vs a full VM with a complete file system and IIS settings to mess with.

I had the same issue and found that the older version had created compiled files in the "bin" folder.

Once I deleted these the error disappeared.

For MVC5 below code works for issue same controller name for different areas. You have to specify which area controller have to hit first.

        routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "ApplicationName.Areas.AreaName.Controllers" }
).DataTokens.Add("area", "AreaName");

This issue occurred when I accidentally added

[Route("api/[controllers]s")]

instead of

[RoutePrefix("api/[controllers]s")]

to the top of my ApiController.