开发者

ASP.NET MVC: Always auto link to a default area, unless an area is specified

开发者 https://www.devze.com 2023-02-07 15:36 出处:网络
I have an existing application that has no areas. However, I am adding a whole new section so I have a created an area. So I have one area and the default area.

I have an existing application that has no areas. However, I am adding a whole new section so I have a created an area. So I have one area and the default area.

So, for example:

Default route = /Invoice/Create

Area route = /TAE/Port/Create

Both these links work. However, when I navigate to the TAE area section, all the links change to be within that TAE area. eg /TAE/Invoice/Create

I can force them to have no area adding new { area = "" } to the action link, but this involves modifying every link in the application.

Is it possible to have an actionlink with no area specified always go to the default area, regardless of what area the current page is in?

global.asax.cs

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

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { area="", controller = "Home", action = "Index", id = "" }, // Parameter defaults
            new string[] { "BankingDB.Controllers" } //na开发者_JS百科mespaces
        );

    }

TAEAreaRegistration.cs

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "TAE_default",
            "TAE/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }

Link to TAE area:

<%= Html.ActionLink("Create", "Create", "Port" , new { area = "TAE"}, new {}) %>

Normal area link:

<%= Html.ActionLink("Create", "Create", "Invoice") %>


Edit: I recommend, that if you going to use an area anywhere, you should make them all in areas.

Otherwise, you can overwrite the extension with that signature and make a default to the area = "" then you would never need to write it :D This is a link to what you would want to overwrite ;D

Extra Edit: Just to make sure you understood the road I gave you. you can easily make a new extension that would call that function with the parameter area blank already filled in. or always just overwrite lol good luck

0

精彩评论

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

关注公众号