【问题标题】:ASP.NET MVC Routing with areas带有区域的 ASP.NET MVC 路由
【发布时间】:2011-10-13 12:33:33
【问题描述】:

我有一个叫做组织的区域。在这个区域中,我有一个带有 Url.Action 链接的视图。

默认情况下,如果我只是传递动作名称,它会在组织区域中调用当前控制器中的动作。

我在控制器文件夹中有一个控制器(不在一个区域中),我想在该控制器中调用一个动作。

基本上,任何区域都可以调用此操作。实现这一目标的最佳方法是什么?

如果这是完全错误的解决方法,那么我愿意接受建议。

谢谢,

编辑 - 这是路线

全球.asax

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Columns",
            "Columns/Columns/{ID}/{idList}",
            new { controller = "Columns", action = "UserColumnList" }
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Account", action = "Login", id = UrlParameter.Optional } // Parameter defaults
        );

    }

OrganisationsAreaRegistration.cs

public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Organisations_view",
            "Organisations/{id}/View",
            new { controller = "Manage", action = "View" }
        );

        context.MapRoute(
            "Organisations_general",
            "Organisations/{id}/General",
            new { controller = "Manage", action = "General" }
        );

        context.MapRoute(
            "Organisations_addressbook",
            "Organisations/{id}/AddressBook",
            new { controller = "Manage", action = "AddressBook" }
        );

        context.MapRoute(
            "Organisations_departments",
            "Organisations/{id}/Departments",
            new { controller = "Manage", action = "Departments" }
        );

        context.MapRoute(
            "Organisations_people",
            "Organisations/{id}/People",
            new { controller = "Manage", action = "People" }
        );

        context.MapRoute(
            "Organisations_events",
            "Organisations/{id}/Events",
            new { controller = "Manage", action = "Events" }
        );

        context.MapRoute(
            "Organisations_journal",
            "Organisations/{id}/Journal",
            new { controller = "Manage", action = "Journal" }
        );

        context.MapRoute(
            "Organisations_tasks",
            "Organisations/{id}/Tasks",
            new { controller = "Manage", action = "Tasks" }
        );

        context.MapRoute(
            "Organisations_edit",
            "Organisations/{id}/Edit",
            new { controller = "Manage", action = "Edit" }
        );

        context.MapRoute(
           "Organisations_journalnew",
           "Organisations/{id}/{action}",
           new { controller = "Manage" }
       );

        context.MapRoute(
            "Organisations_recent",
            "Organisations/{action}",
            new { controller = "Manage", action = "Index" }
        );

        context.MapRoute(
            "Organisations_default",
            "Organisations/{controller}/{action}/{id}",
            new { controller = "Manage", action = "Index", id = UrlParameter.Optional }
        );


    }

【问题讨论】:

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


    【解决方案1】:

    其实很简单。在Url.Action 中,只需添加一个空白的area 值,如下所示:

    @Url.Action("Index", "Home", new { area = "" })
    

    在解析链接时,MVC会识别出目标不在区域内,不会使用任何区域路由来生成链接。

    【讨论】:

    • 工作愉快,有时事情很简单!谢谢。
    【解决方案2】:

    您需要为该控制器/操作创建一个特定的路由才能使其工作,并且我会将其放置在其他路由之上(除非它足够具体以至于其他路由不会命中它)。

    我很乐意通过代码向您展示这一点,但我们需要查看您当前的路由表和有问题的控制器。

    【讨论】:

    • 谢谢乔治。上面添加了路线。目前,我对包含动作的每个区域都有一个非常讨厌的方法,然后运行 ​​return RedirectPermanent("~/Columns/UserColumnList");
    猜你喜欢
    • 2010-12-09
    • 2018-05-31
    • 1970-01-01
    • 2021-02-13
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多