具有相同名称和区域但找到“多种类型”的MVC控制器

时间:2015-02-03 13:33:36

标签: c# asp.net-mvc asp.net-mvc-areas

我对区域和控制器有一点问题。 我有一个名为" HomeController"这是默认控制器,然后我添加了一个名为" Supplier"的新区域,然后我复制了我的HomeController并删除了我不想编辑的所有方法,只是编辑了我的索引方法。 / p>

现在当我构建它工作正常但是当我作为供应商去我的家庭控制器时它会出现这个错误

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:
TestProject.Controllers.Home
TestProject.Areas.Supplier.Controllers.Home 

我更新了我的区域

这是我的默认区域

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[] { "TestProject.Controllers" }
            );
        }
    }

这是我的区域路线文件

public class SupplierAreaRegistration: AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Supplier";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {

            context.MapRoute(
                "SupplierHomeIndex",
                "Home/Index/{id}",
                defaults: new { area = "Supplier", controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "TestProject.Areas.Supplier.Controllers" },
                constraints: new { permalink = new AreaConstraint(AreaName) }
            );

            context.MapRoute(
                "SupplierDefault",
                "{controller}/{action}/{id}",
                defaults: new { area = "Supplier", action = "index", id = UrlParameter.Optional },
                namespaces: new[] { "TestProject.Controllers"},
                constraints: new { permalink = new AreaConstraint(AreaName) }
            );
        }
    }

任何人都可以对此有所了解吗?我通过Google和Stackoverflow查看了许多主题和答案,但似乎没有什么对我有用。

1 个答案:

答案 0 :(得分:1)

您已自定义了区域的路由,并删除了Supplier网址前缀。当路由框架旋转时,它只收集应用程序中的所有控制器,无论它们在何处,然后根据URL查找匹配项。在您的情况下,您现在有两个控制器都绑定到URL /Home/*。通常,区域的网址会以区域的名称为前缀,以避免混淆,即/Supplier/Home