5

How to setup route for a view to be as home page of a domain in ASP.NET MVC application which contains Areas. I need a view of a particular area to be home page. How could this be done?

I tried using the following code without any success.

public static void RegisterRoutes(RouteCollection routes) {
            routes.MapRoute(
                name: "Home",
                url: "",
                defaults: new { controller = "Home", action = "Index" }, 
                namespaces: new string[] { "WebApp.Areas.UI.Controllers" }
                );
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Brij
  • 11,731
  • 22
  • 78
  • 116
  • possible duplicate of [How to set a Default Route (To an Area) in MVC](http://stackoverflow.com/questions/2140208/how-to-set-a-default-route-to-an-area-in-mvc) – Chris Moschini Apr 12 '15 at 19:07
  • This is a dupe, and the only answer is incorrect; the other question includes a note that the below answer won't actually accomplish what the asker here is asking to do, using an Area to serve the root page ("/"). It has to be routed from the main RouteCollection, and can't be done from an Area as the answer below implies. – Chris Moschini Apr 12 '15 at 19:09

1 Answers1

4

In the Area folder there is a file by name AreaNameAreaRegistration deriving from AreaRegistration, it has a function RegisterArea which sets up the route.

Default route in an Area is AreaName/{controller}/{action}/{id}. Modifying this can set an area as default area. For example I set the default route as {controller}/{action} for my requirement.

public class UIAreaRegistration : AreaRegistration
{
        public override string AreaName
        {
            get
            {
                return "UI";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "UI_default",
                "{controller}/{action}/{id}", //******
                new {controller = "Home", action = "Index", id = UrlParameter.Optional}
            );
        }
    }
Chris Moschini
  • 36,764
  • 19
  • 160
  • 190
Brij
  • 11,731
  • 22
  • 78
  • 116