【问题标题】:MVC5 application with areas and subfolders within the controller folder控制器文件夹中具有区域和子文件夹的 MVC5 应用程序
【发布时间】:2014-11-17 21:36:54
【问题描述】:

背景故事:
我的公司有几个大型的独立应用程序,我们将它们合并为一个应用程序。在研究的过程中,我使用了一个 MVC 应用程序,每个应用程序都位于区域文件夹中。所以结构是

  • 应用
    • 区域
      • 应用程序 1
        • 控制器
          • WorkWithReceipts
            • 收据控制器
            • SomethingElseController
          • 文件维护
            • 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" }
        );
    }

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

【问题讨论】:

    标签: asp.net-mvc routing


    【解决方案1】:

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

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

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

    【讨论】:

    • 我很抱歉不清楚。我想知道如何设置我的路由,以便我的路由期望相同的参数,但是,根据传入的区域、控制器和操作,它会找到要使用的正确路由。现在它总是使用最顶层的路线。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2017-10-18
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 2011-12-11
    相关资源
    最近更新 更多