C# 发现多种类型与名为“Home”的控制器相匹配 - 在两个不同的区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8788014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:25:52  来源:igfitidea点击:

Multiple types were found that match the controller named 'Home' - In two different Areas

c#asp.net-mvcasp.net-mvc-3asp.net-mvc-routing

提问by fiberOptics

I have two areas in my project. Now when I run the program I get this error:

我的项目中有两个领域。现在,当我运行程序时,出现此错误:

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:
BaseAdminMVC.Areas.BaseAdmin.Controllers.HomeController
BaseAdminMVC.Areas.TitomsAdmin.Controllers.HomeController  

I have found some source here: Multiple Controller name
But I think it only works for one area.
In my case I have two projects in different areas. Hope someone could tell what should I do to solve the problem.
Here is the Global.asaxfile:

我在这里找到了一些来源:Multiple Controller name
但我认为它只适用于一个区域。
就我而言,我在不同领域有两个项目。希望有人能告诉我该怎么做才能解决问题。
这是Global.asax文件:

public static void RegisterRoutes(RouteCollection routes)
        {
            string[] namespaces = new string[] { "BaseAdminMVC.Areas.BaseAdmin.Controllers", "BaseAdminMVC.Areas.TitomsAdmin.Controllers"};

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces
            );
        }  

By the way, I have also controller ("HomeController") outside the Areafolder. This just provides links to two projects BaseAdminand TitomsAdmin.

顺便说一句,我还在文件夹HomeController外有控制器(“ ”)Area。这仅提供指向两个项目BaseAdminTitomsAdmin.

I have tried this solution, but still doesn't work:

我已经尝试过这个解决方案,但仍然不起作用

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
                "BaseAdmin",
                "BaseAdmin/{controller}/{action}",
                new { controller = "Account", action = "Index" },
                new string[] { "BaseAdminMVC.Areas.BaseAdmin.Controllers" }
            );

            routes.MapRoute(
                "TitomsAdmin",
                "TitomsAdmin/{controller}/{action}",
                new { controller = "Home", action = "Index" },
                new string[] { "BaseAdminMVC.Areas.TitomsAdmin.Controllers" }
            );

Thanks in advance!!

提前致谢!!

采纳答案by fiberOptics

I don't know what happen, but this code works fine:

我不知道发生了什么,但这段代码工作正常:

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 },
        new string[] { "BaseAdminMVC.Areas.TitomsAdmin.Controllers" }
    );
}

回答by Justin Ipson

I got this error after doing a rename of my project namespace/assembly.

重命名我的项目命名空间/程序集后出现此错误。

If you renamed the Namespace/Assembly you might have a leftover assembly/dll from the previous name in your bin folder. Just delete it from there and it should work.

如果您重命名了命名空间/程序集,您的 bin 文件夹中可能会有一个来自以前名称的剩余程序集/dll。只需从那里删除它,它应该可以工作。

回答by VivekDev

Right click the project and select clean the project. Or else completely empty the bin directory and then re-build again. This should clear of any left over assemblies

右键单击项目并选择清理项目。或者完全清空 bin 目录,然后重新构建。这应该清除任何剩余的组件