0

I have one area Area1 with controller Home and it's Index method.
I also have created another area Area2 with controller Home and it's Index method.
In Area1 I have action link that should open Index page in Area2.

@Html.ActionLink("Link to another area index", "Index", "Home", new { area = "Area2" }, null)

But when I click on this link it first go to Area1/Home/Index!
Why is this happening. Is this has to be like this or it can go directly to Area2/Home/Index?
This makes me problem because in Area1/Home/Index I need some parameters and when this happens this valuer are null or wrong and make me problem. I must doing something wrong. Update Area1 routing

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.Routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

            context.MapRoute(
                "Area1_home",
                "{country}/{city}",
                new { controller = "Home", action = "Index", country = UrlParameter.Optional, city = UrlParameter.Optional }
            );

            context.MapRoute(
                "Area1_default",
                "Area1/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

Area2 routing:

public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Area2_default",
                "Area2/{controller}/{action}/{id}",
                new { controller="Home", action = "Index", id = UrlParameter.Optional }
            );
        }
1110
  • 7,829
  • 55
  • 176
  • 334

1 Answers1

1

Try:

Html.ActionLink("Link to another area index", "Index", "Home", new { Area = "Area2" }, new{});
Nope
  • 22,147
  • 7
  • 47
  • 72
  • Still goes to Area1/Home/Index first :( – 1110 Feb 10 '12 at 20:42
  • Hmm, that's strange, I found some more links saying pretty much the same thing: http://www.f15ijp.com/2011/04/30/specifying-what-area-an-actionlink-should-use/ and also here http://stackoverflow.com/questions/2036305/how-to-specify-an-area-name-in-an-action-link. If you got your areas set up right and no special routing configured I don't know then why it would not work. You did try with the word `Area` spelled with a capital `A` yes ? – Nope Feb 10 '12 at 20:48
  • Yes I have tried 'A'. I have updated question with routing because maybe routing is making my trouble. – 1110 Feb 10 '12 at 20:53
  • When I created project I removed all routings from global.asax. I return default routing now and it worked. I don't understand why this works... routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { area = "Area1", controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); – 1110 Feb 10 '12 at 21:28
  • At least you got your action link to work to work and can focus on the routing now. Routing can get very confusing. Remember that a route is always tried to be matched top down. So, which ever rout configuration is added first will the first one tried to be matched firsted. If MVC finds a match it will not continue to search and return that route configuration. When adding routes always add them with most important to match fist and with least important last. – Nope Feb 10 '12 at 21:37