asp.net – MVC4默认路由指向某个区域
发布时间:2020-12-16 04:35:10 所属栏目:asp.Net 来源:网络整理
导读:我的项目中有两个区域: Areas | AdminAreas | FrontEnd 我想要的是当我访问该站点时,默认路由应该从FrontEnd区域加载Controllers / Views / Models.为管理员面板设置Url / Admin是正常的,但我宁愿不必强制Url / FrontEnd(或其他一些变体).基本上我不想在根
我的项目中有两个区域:
Areas | Admin Areas | FrontEnd 我想要的是当我访问该站点时,默认路由应该从FrontEnd区域加载Controllers / Views / Models.为管理员面板设置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",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 我确实有区域的视图,这看起来不像它在那里. 解决方法
我相信你可以这样做:
// 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",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",defaults: new { area = "FrontEnd",id = UrlParameter.Optional }); } } // Global.asax.cs protected void Application_Start() { AreaRegistration.RegisterAllAreas(); ... } 现在,在RouteConfig类中,您可能已设置了默认路由.请记住,只要在调用RouteConfig.RegisterRoutes之前调用AreaRegistration.RegisterAllAreas,您在区域中设置的路由可能会覆盖您在RouteConfig中设置的路由. (路由按它们在Routes集合中出现的顺序进行评估,而.MapRoute将新路由推送到最后) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.net会话状态服务信息
- asp.net-mvc-3 – 如果Controller.OnAuthorization()返回vo
- asp.net-mvc – Html.CheckBox即使模型值为true也不进行检查
- MVC SessionStateAttribute不作为全局属性
- asp.net – ASP Core,Identity Server 4和Angular 2中的外部
- asp.net – 禁用javascript中的复选框,并在服务器端将其识别
- 冻结asp.net网格视图列
- asp.net – 什么导致页面呈现慢?
- asp.net-mvc – 如何在razor(CSHTML)中设置contenttype?
- ASP.NET MVC CAPTCHA实现
推荐文章
站长推荐
- dotnetcore3.1 WPF 实现多语言
- 清除ASP.net窗体中的所有字段
- asp.net-mvc – 如何在会话较少的负载平衡环境中
- asp.net-mvc – ASP.NET MVC:很多路由 – >总是
- asp.net-mvc – 绕过asp.net mvc中特定类型的Dat
- 为什么ASP.NET MVC中的移动视图在不同的服务器上
- asp.net-core – CoreCLR中的MD5CryptoServicePr
- asp.net-mvc-2 – 强大类型的ActionLink在Asp.Ne
- asp.net – IIS在编译的.net站点中查找.cs文件
- asp.net-mvc – 在ASP.NET MVC中封装用户控件
热点阅读