Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 - 2 Areas With Segments, 1 Area Without

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.

like image 946
Bern Avatar asked Oct 24 '22 03:10

Bern


1 Answers

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.

like image 194
danludwig Avatar answered Oct 29 '22 21:10

danludwig