ASP.NET MVC 4 - 2 areas with segments, 1 zone without

I just created a new ASP.NET MVC 4 solution and added 3 Scopes and would like them to be 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 routing so that the "General" area works when it is in the first segment, but it cannot seem that my routing works in all 3 areas when I do not want the "General" to appear as a segment in the URL. As you can see, I am aiming for a clean URL structure.

I plan to add several controllers / views in each area and would like to support this organization of areas.

I saw a similar MVC 2 problem , but not sure if streamlining the area registrations will fix my specific problem.

+5
source share
1 answer

Open the 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 a URL http://www.mysite.com/members, and suppose this is in the MembersAreaRegistration.cs file:

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

... then this should work. However, if you do not have a snippet controller = "Home"in MapRoute's default settings, then the URL should be http://www.mysite.com/members/home.

+3
source

All Articles