MVC3 Routing with Areas

1.4k Views Asked by At

I have a MVC3 application with two areas and a root area. The general structure looks like Root

- Root/Areas/Manager
    * Views/Home/Index.cshtml
    * ManagerAreaRegistration.cs
- Root/Areas/Participant
    * Views/Home/Index.cshtml
    * ParticipantAreaRegistraion.cs
- Root
    * Views/Home/Index.cshtml
    * Views/Account/Register.cshtml
    * Global.asax.cs

I am having two problems with routing. The first is that I am unable to navigate to any pages in the Root/Views folders except the one set as default in the Global.asax.cs file. The Global.asax.cs file looks like:

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[] { "MVCApplication.Controllers" }    // for areas 
   );
...     
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
...

And the code in Root/Views/Home/Index.cshtml which is the start page looks like:

@Html.ActionLink("Accounts","Register", new {area="", controller="Accounts"},null)
@Html.ActionLink("Manager", "Index", new { area = "Manager", controller = "Home" })
@Html.ActionLink("Participant", "Index", new { area = "Participant", controller = "Home" })

The two area links work fine as I have added routes into the registration files in each area, but the link to Accounts/Register which is another page in the root gives a 'resources not found error'. However, if I change the Global.asax.cs route to have

    new {controller="Accounts" , action = "Register", id = UrlParameter.Optional }, 

in the default route, then I can start on the Register page.

So my first question is: How do I use routes to be able to access both pages in the Areas and in the Root (ie the Accounts/Register page)?

My second question has to do with the areas themselves. Since they both have a 'Home' controller, I have put the area name in front of one to distinguish it, but I would like to not have to do this. Currently the 'ParticipantAreaRegistration.cs file has the code:

  context.MapRoute(
            "Participant_default",
            "Participant/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
          new[] { "MvcApplication.Areas.Participant.Controllers"  }    // for areas 
            );

which gives URL's of "localhost**/Participant/Home"

while the ManagerAreaRegistraion.cs has code

 context.MapRoute(
            "Manager_default",
            "{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
                         new[] { "MvcApplication.Areas.Manager.Controllers" }    // for areas 

        );

which gives URL's of "localhost***/Home/"

My second question is : How can I have the URL of "localhost**/Home for both Manager and Participant (or for any number of areas) without having to have the Area name displayed in the URL?

I know this question is similar to others already on file, but I have scoured these to no avail and am currently drowning in inefficiency, so I thought I would try asking with specificity. Thanks.

1

There are 1 best solutions below

1
On

You can use custom routing.

Something similar to this one:

MVC 2 AreaRegistration Routes Order

Using the solution in the above problem, you can write custom order of routing.

In one of my application, I have areas named Admin,Blog,Members and Public. I have routed the Public area as the url: http://localhost:4000/, Admin as: http://localhost:4000/admin, blog as: http://localhost:4000/blog, etc.. If you want my code, I can give you.