5

I've just created a fresh ASP.NET MVC 4 solution and have added 3 Areas and would like them routed as indicated:

1. General         -> http://www.mysite.com/
2. Members         -> http://www.mysite.com/members/
3. Administration  -> http://www.mysite.com/administration/

I can configure the routing so that "General" Area works when it's the first segment but can't seem to get my routing working across all 3 Areas when I don't want "General" to appear as a segment in the URL. As you can see I'm aiming for a clean URL structure.

I plan on adding a number of controllers/views under each area and would like to maintain this organisation of Areas.

I've seen a similar MVC 2 problem posted but am not sure the ordering of area registrations will correct my particular issue.

Community
  • 1
  • 1
Bern
  • 7,102
  • 5
  • 31
  • 46
  • Does "General" really need to be an area? It seems like that should be the bulk of your site (not in any area) and then "Members" and "Administration" are the two areas. – Ethan Brown Apr 02 '12 at 19:00
  • Hi Ethan. There is that option but ideally I'd like to keep all controllers/views within an area as the "General" area will, over time, become quite large. – Bern Apr 02 '12 at 19:32
  • Or add to your areas in namespaces.Try this like:`context.MapRoute( name: "Dashboard_Site", url: "Dashboard/{controller}/{action}/{id}", defaults: new { area = "Dashboard", controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new {"WebSite.Areas.Dashboard.Controllers"});` – Elyor Apr 30 '13 at 06:45

1 Answers1

3

Open your GeneralAreaRegistration.cs file.

Find this:

context.MapRoute(
    "General_default",
    "General/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);

...and replace with this:

context.MapRoute(null,
    "{controller}/{action}/{id}",
    new { controller = "General", action = "Index", id = UrlParameter.Optional }
);

Reply to comments:

Assuming you are using the URL http://www.mysite.com/members, and assuming this is in your MembersAreaRegistration.cs file:

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

...then it should work. However, if you don't have the controller = "Home" fragment in your MapRoute defaults, then the URL would have to be http://www.mysite.com/members/home.

danludwig
  • 45,241
  • 21
  • 150
  • 230
  • That certainly solved the URL issue (thanks), however my very simple HomeController in the "Members" Area returns a 404 now. Is some adjustment required for the default `.MapRoute()` calls? – Bern Apr 02 '12 at 20:51
  • Hello. That is exactly as it is in my code so there is no change as the 404 still appears. Should I be manaually registering the Members area before the General area in the Global.asax? – Bern Apr 03 '12 at 05:10
  • @Bern no, I don't think so. All areas should be registered by the `AreaRegistration.RegisterAllAreas()` line in global.asax. You didn't change any of the area namespaces, did you? If you go back to your original MapRoute for the General area, does the 404 problem go away? – danludwig Apr 03 '12 at 05:18
  • Thanks for your help @danludwig, I eventually got the bottom of this today. In **VS2010 using .NET 4** I could resolve the issue using your code. In **VS2011 Beta using .NET 4.5** I had to do 2 things **(1)** manually register each area in the Global.asax and ensure the "General" area was registered last - by default it registers the areas alphabetically (problem). If I created an area called "Aardvark" it was fine __, and **(2)** explicitly add the namespaces to the MapRoute() calls. Apologies for not stating my original IDE and framework version earlier, and thanks for your help. – Bern Apr 03 '12 at 15:18