Search code examples
c#asp.net-mvcrazorhtml-helperasp.net-mvc-areas

MVC Areas Routing


I have an MVC Application as follows:

Application
 Controllers
      HomeController
           ActionResult Index();
 Areas
      IT
           Controllers
                ITHomeController
                     ActionResult Index();

I have setup the links to work correctly between the main application and the IT Area.

In the URL the link to the IT area shows up as http://localhost/IT/ITHome. The code for the link is:

@Html.ActionLink("IT Page","Index","ITHome", new { area = "IT" }, null)

How would I have the url simply be http://localhost/IT I'm sure it's simple.

Thanks!


Solution

    1. Look for /Areas/IT/ITAreasRegistration.cs
    2. Add a route mapping in its public override void RegisterArea(AreaRegistrationContext context) method, preferably at the very top or at least before the default route for the area.

    Something like:

    context.MapRoute(
                    "default_IT",
                    "IT",
                    new { action = "Index", controller = "ITHome" },
                    new[] { "YourAppNamespaceHere.Areas.IT.Controllers" }
                );
    
    1. As for the link... I'd just hand code it to <a href="/IT">IT Home</a> and be done with it unless you are hosting this as a sub application from the wwwroot, you should be fine.

    In this way you can keep your area routes separate from your normal routes.