【问题标题】:MVC controllers with same name with areas but finds "Multiple types"MVC 控制器与区域同名但发现“多种类型”
【发布时间】:2015-02-03 13:33:36
【问题描述】:

我自己在区域和控制器方面有点问题。 我有一个名为“HomeController”的控制器,这是默认控制器,然后我添加了一个名为“Supplier”的新区域,然后我复制了我的 HomeController 并删除了所有我不想编辑的方法,只编辑了我的 Index 方法。

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

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 查看了许多主题和答案,但似乎没有什么对我有用。

【问题讨论】:

  • RegisterRoutes 中的namespaces: new[] { "TestProject.Controllers" } 更新为namespaces: new[] { "TestProject.Areas.Supplier.Controllers" }
  • 这不会消除我的供应商限制吗?
  • 您的操作链接是否定义了区域? @Url.Action("action", "controller", new {area = "areaname"}) ?
  • 我有,但似乎仍然出现同样的错误

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


【解决方案1】:

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

【讨论】:

  • 我已经像这样更新了我的路线 context.MapRoute( "SupplierHomeIndex", "Supplier/Home/Index/{id}", defaults: new { area = "Supplier", controller = "Home" , action = "Index", id = UrlParameter.Optional }, 命名空间: new[] { "TestProject.Areas.Supplier.Controllers" }, 约束: new { permalink = new AreaConstraint(AreaName) } );但似乎仍然出现“多种类型”错误
  • Both 路线需要更新。你有这条路线,加上该地区的默认路线。
猜你喜欢
  • 2020-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-19
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多