MVC5应用程序,包含控制器文件夹中的区域和子文件夹

时间:2014-11-17 21:36:55

标签: asp.net-mvc routing

返回故事:
我的公司有几个大型的独立应用程序,我们正在整合到一个应用程序中。在研究时,我已经使用了MVC应用程序,每个应用程序都将位于areas文件夹中。所以结构是

  • 应用
      • 申请1
        • 控制器
          • WorkWithReceipts
            • ReceiptsController
            • SomethingElseController
          • FileMaintenance
            • MainSetupController
      • 申请2
        • 控制器
            • TreesController
            • TreesController

问题
所以我遇到的问题是我的路由。它总是使用顶部的,如果我尝试访问MainSetupController,它会搜索Controllers / WorkWithReceipts / MainSetup / [action],它会因为它位于FileMaintenance文件夹下而中断。

这是我的路由配置。

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "FileMaintenance",
            "Accounting/FileMaintenance/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Courts.Areas.Accounting.Controllers.FileMaintenance" }
        );
        context.MapRoute(
            "Receipts",
            "Accounting/Receipts/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Courts.Areas.Accounting.Controllers.Receipts" }
        );

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

问题
所以我想知道是否有办法设置我的路由,以便我的路由期望相同的参数,但是,根据传入的区域,控制器和操作,它找到正确的路由使用。现在它总是使用最顶层的路线。

1 个答案:

答案 0 :(得分:0)

你的问题并不完全清楚,但我会尝试覆盖基础。如果您的问题是使用Html.ActionLink之类的内容生成正确的网址,则需要将Area作为路由参数传递。例如:

@Html.ActionLink("Foo", "FooAction", "FooController", new { Area = "FooArea" })

接下来,命名空间有助于限制到区域/控制器等的特定路由,但它不会反向工作。也就是说,尝试解析Url.Action("Index", "Foo", new { Area = "Bar" })之类的网址永远不会导致Accounting/FileMaintenance/Foo/Bar/1,因为该路由仅限于Courts.Areas.Accounting.Controllers.FileMaintenance命名空间,但是,直接请求Accounting/FileMaintenance/Foo/Bar/1,如果食物链中较高的另一条路线首先匹配,则不一定会解析到该特定路线。

相关问题