【问题标题】:MVC Routing with Matching Controllers in Areas: Multiple types were found that match the controllerMVC Routing with Matching Controllers in Areas: 发现多种类型与控制器匹配
【发布时间】:2014-04-07 20:55:03
【问题描述】:

我有一个包含几个方面的 MVC 应用程序。每个区域,以及根,都有一个 HomeController。

/Home/Index
/Admin/Home/Index
/Content/Home/Index

所有这些都可以正常工作并正确确定要使用哪个家庭控制器。

我的问题是每个区域也有 ReportsController,但根没有。

/Admin/Reports/Index
/Content/Reports/Index

这两项工作都按预期工作,但我收到 URL /Reports/Index 的“找到与控制器匹配的多种类型”错误。它在默认路线上匹配,但溢出到区域。我的假设是它应该是 404,就像去一条从所有区域都丢失的路线一样。

我的路由配置如下:

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

context.MapRoute(
    "Content",
    "Content/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home", id = UrlParameter.Optional },
    namespaces: new[] { "Website.Areas.Content.Controllers" }
);

context.MapRoute(
    "Admin",
    "Admin/{controller}/{action}/{id}",
    new { action = "Index", controller = "Home", id = UrlParameter.Optional },
    namespaces: new[] { "Website.Areas.Admin.Controllers" }
);

问题路由 (/Reports/Index) 在默认路由映射上匹配,但不仅限于 Website.Controllers 的定义命名空间,它还在这些区域中查找。相反,它应该将搜索限制在 Website.Controllers 并在没有找到 ReportsController 时返回 404。

【问题讨论】:

    标签: asp.net asp.net-mvc routes asp.net-mvc-routing


    【解决方案1】:

    如下文所述,命名空间用于确定优先级,但可以通过禁用回退查找来限制命名空间,如下所示:

    routes.MapRoute(...).DataTokens["UseNamespaceFallback"] = false;

    http://bubblogging.wordpress.com/2012/06/09/mvc-routing-namespaces/

    【讨论】:

      猜你喜欢
      • 2014-05-19
      • 2023-04-06
      • 1970-01-01
      • 2021-06-06
      • 2019-05-12
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2016-07-27
      相关资源
      最近更新 更多