【问题标题】:MVC3: One controller, multiple routesMVC3:一个控制器,多条路由
【发布时间】:2012-01-08 14:06:52
【问题描述】:

我有一个包含多个区域的 MVC3 应用程序。每个区域都是一个不同的应用程序模块,但它们共享一个共同的功能——报告。因此,我在根区域编写了一个报告控制器和视图,并希望使用路由将它们附加到所有区域。此方法在 MVC1 上运行良好,但我随后升级到 MVC3,路由停止工作并始终返回 404。

每个模块/区域的路由结构都相似:

Module1/Reports.aspx/
Module1/PreportView.aspx/{id}
Module1/{controller}/{action}
Module1/{controller}/{action}/{id}

模块的路由注册是通过以下方法进行的:

protected override void RegisterRoutes(AreaRegistrationContext context, string arearoot, string defaultControllerName){
  context.MapRoute(arearoot + "Reports", arearoot + "/Reports.aspx",
    new { action = "Index", controller = "Reports" }, new string[] { "Controllers" }); //this should work in MVC3 according to docs

  context.MapRoute(arearoot + "ReportView", arearoot + "/ReportView.aspx/{id}",
    new { action = "Show", controller = "Controllers.ReportsController" });//this method worked in MVC1

  context.MapRoute(arearoot,
    arearoot + "/{controller}.aspx/{action}",
    new { controller = defaultControllerName, action = "Index" },
    GetRouteNamespaces());

  context.MapRoute(arearoot + "ItemSpecific",
    arearoot + "/{controller}.aspx/{action}/{id}");
}

protected string[] GetRouteNamespaces() {
  return new string[] { "Controllers.Module1" }; //returns proper namespace for each module
}

我尝试附加 RouteDebugger(由于 .aspx 扩展名而失败)和 Glimpse,但它们都没有显示任何问题,路由表似乎没关系,但是当我尝试导航到报告时,我总是收到 404 错误代码。

有什么想法吗?

【问题讨论】:

  • 如何调用这些操作(在您的 aspx/c 文件中)?

标签: c# asp.net-mvc-3 routes


【解决方案1】:
  • 请检查您区域目录中的区域注册路线。
    • 在调试过程中还可以获取所有注册路由列表。如果您使用的是 ASP.net MVC 2 或更高版本,路由会在许多地方注册 区域。
      • 您还必须在注册路线时指定区域。 (与您指定控制器的方式相同)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多