6

I am using MVC3 and have Areas in my application. In general, everything works fine, I can navigate to my area (e.g. Admin=Area, Controller=Department) like this:

<%: Html.ActionLink("Departments", "DepartmentIndex", "Department", new { area = "Admin"}, null )%>

However, what I noticed is that if I don't specify the area in my ActionLink, e.g.

<%: Html.ActionLink("Test", "Index", "Home")%>

This will stop working if I have navigated to the "Admin" area. i.e. my url is now http://localhost/myproj/Admin/Home/Index instead of http://localhost/myproj/Home/Index

Any ideas on what is going on here?

My routes are all the defaults when creating an MVC app / Areas. i.e.

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

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
            new[] { "MyProj.Controllers" } 
        );
    }

And my area registration

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

Is this by design? This posting suggests using RouteLink instead of ActionLink: say to use RouteLink Is it required to have "area" routevalue on actionlink if the application was grouped into areas?

Community
  • 1
  • 1
Raymond
  • 3,382
  • 5
  • 43
  • 67
  • 1
    Is this by design? Maybe you have to explicitly specify Areas=""? – Raymond May 25 '11 at 23:47
  • possible duplicate of [Is it required to have "area" routevalue on actionlink if the application was grouped into areas?](http://stackoverflow.com/questions/5549497/is-it-required-to-have-area-routevalue-on-actionlink-if-the-application-was-gr) – Pure.Krome Nov 03 '11 at 08:27

1 Answers1

8

This StackOverflow answer correctly states that you need to provide the Area name if you're using areas.

eg.

Html.ActionLink("Home", "Index", "Home", new { Area = "" }, new { })
Community
  • 1
  • 1
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647