MVC4默认路由指向某个区域

时间:2013-09-26 21:25:15

标签: asp.net asp.net-mvc-4 routing

我的项目中有两个区域:

Areas | Admin
Areas | FrontEnd

我想访问网站时,默认路由应该从FrontEnd区域加载控制器/视图/模型。对于管理面板而言Url/Admin是正常的,但我宁愿不必强迫Url/FrontEnd(或其他一些变体)。基本上我不想在根级别使用Controller / Model / View文件夹。

我不确定如何更改代码以允许此操作,甚至是一种可行的方法。有人可以提供一些指导吗?

我有什么:

routes.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { 
                    area = "Admin",
                    controller = "Home", 
                    action = "Index", 
                    id = UrlParameter.Optional 
                },
                namespaces: new[] { "WebsiteEngine.Areas.Admin.Controllers" }
            );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { 
                    area = "FrontEnd", 
                    controller = "Home", 
                    action = "Index", 
                    id = UrlParameter.Optional 
                },
                namespaces: new[] { "WebsiteEngine.Areas.FrontEnd.Controllers" }
            );

然而,这会产生错误:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

我确实在这些区域有可用的视图,这看起来不像是在那里。

1 个答案:

答案 0 :(得分:10)

我相信你可以做这样的事情:

// Areas/Admin/AdminAreaRegistration.cs
public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get { return "Admin"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            name: "Admin_Default", 
            url: "Admin/{controller}/{action}/{id}", 
            defaults: new 
            {
                area = "Admin",
                controller = "Home", 
                action = "Index", 
                id = UrlParameter.Optional 
            });
    }
}


// Areas/Admin/FrontEndAreaRegistration.cs
public class FrontEndAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get { return "FrontEnd"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            name: "FrontEnd_Default", 
            url: "{controller}/{action}/{id}", 
            defaults: new 
            {
                area = "FrontEnd",
                controller = "Home", 
                action = "Index", 
                id = UrlParameter.Optional 
           });
    }
}

// Global.asax.cs
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    ...
}

现在,在RouteConfig课程中,您可能已设置Default路线。请记住,只要您在之前拨打AreaRegistration.RegisterAllAreas ,即可拨打RouteConfig.RegisterRoutes,您在区域中设置的路线可能会覆盖您在{{1}中设置的路线}}。 (路由按照它们在RouteConfig集合中出现的顺序进行评估,Routes将新路由推送到最后)