ASP.NET MVC路由区域

时间:2011-10-13 12:33:33

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

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

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

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

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

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

谢谢,

编辑 - 这是路线

Global.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 }
        );


    }

2 个答案:

答案 0 :(得分:1)

实际上很容易。在Url.Action中,只需添加一个空白area值,如下所示:

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

在解析链接时,MVC将识别目标不在某个区域中,并且不会使用任何区域路径来生成链接。

答案 1 :(得分:0)

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

我很高兴通过代码向您展示,但我们需要查看您当前的路线表和相关控制器。

相关问题