开发者

ASP.NET MVC Routing with areas

开发者 https://www.devze.com 2023-04-12 14:26 出处:网络
I have an area called Organisations. In this area I have a view with a Url.Action link. By default, if I just pass the action name, it will call the action in the current controller in the Organisati

I have an area called Organisations. In this area I have a view with a Url.Action link.

By default, if I just pass the action name, it will call the action in the current controller in the Organisation area.

I have a controller in the controllers folder (not in an area), and I want to call an action in that controller.

Basically, this action will be something that any area can call. What would the best way of achieving this?

If this is totally the wrong way to go about this, then I'm open to suggestions.

Thanks,

EDIT - Here are the routes

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",
            "Organ开发者_StackOverflow中文版isations/{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 }
        );


    }


It's actually quite easy. In Url.Action, just add a blank area value, like so:

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

In parsing the link, MVC will recognize that the target is not in an area, and will not use any area routes to generate the link.


You would need to make a specific route for that controller/action for this to work, and I'd place it above the other routes (unless it's specific enough that other routes won't hit it).

I'd be glad to show you this through code, but we'd need to see your current routes table and the controllers in question.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号