Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Area view as home page in ASP.NET MVC?

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" }
                );
}
like image 242
Brij Avatar asked Oct 05 '22 10:10

Brij


1 Answers

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}
            );
        }
    }
like image 188
Brij Avatar answered Oct 10 '22 03:10

Brij