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

谁说胖子不能爱 提交于 2019-12-09 13:31:51

问题


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" }
                );
}

回答1:


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}
            );
        }
    }


来源:https://stackoverflow.com/questions/14175736/how-to-set-area-view-as-home-page-in-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!