5

如何在包含区域的 ASP.NET MVC 应用程序中将视图设置为域的主页。我需要一个特定区域的视图作为主页。怎么可能做到这一点?

我尝试使用以下代码但没有成功。

public static void RegisterRoutes(RouteCollection routes) {
            routes.MapRoute(
                name: "Home",
                url: "",
                defaults: new { controller = "Home", action = "Index" }, 
                namespaces: new string[] { "WebApp.Areas.UI.Controllers" }
                );
}
4

1 回答 1

4

在 Area 文件夹中有一个名为AreaName AreaRegistration 的文件,该文件来自 AreaRegistration,它有一个函数 RegisterArea 来设置路由。

区域中的默认路由是AreaName /{controller}/{action}/{id}。修改它可以将一个区域设置为默认区域。例如,我将默认路由设置为 {controller}/{action} 以满足我的要求。

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}
            );
        }
    }
于 2013-01-13T07:58:33.367 回答