【问题标题】:MVC4 Default Route to point to an areaMVC4默认路由指向一个区域
【发布时间】:2013-10-03 01:20:11
【问题描述】:

我的项目中有 2 个领域:

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

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

【问题讨论】:

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


    【解决方案1】:

    我相信你可以这样做:

    // 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,您在区域中设置的路线可能会覆盖您在RouteConfig 中设置的路线。 (路由按照它们在Routes 集合中出现的顺序进行评估,.MapRoute 将新路由推到最后)

    【讨论】:

    • 我确实尝试过类似的方法,但我可能弄错了。谢谢,我会告诉你进展如何。
    • 你能看看我的编辑吗?注意:我必须添加命名空间,因为它抱怨 Home 被定义了两次。
    • @webnoob 如果您在各自的 AreaRegistration 类中定义这些路由,看起来它会起作用。我已经对此进行了测试,它似乎有效(至少对我而言)。请参阅我的更新答案。
    • 你是否也删除了RouteConfig.cs 中的默认路由映射?
    • @webnoob 是的,这就是我的意思,但可能没有必要。我以为默认路由会覆盖任何区域路由,结果发现如果你先调用AreaRegistration.RegisterAllAreas,区域路由优先。
    猜你喜欢
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多