我们的网站上有一个区域,人们可以在该区域注册并在我们想要在〜/ pageSlug上托管的网站上获得他们自己的页面.我试过在Global.asax中使用规则,但这打破了允许〜/ Controller直接映射到Index操作的基本默认路由.我不被允许在userSlug前放置任何类型的分隔符,所以〜/ p / pageSlug在这里不是一个真正的选项.
在将用户页面添加到路由方面,我正在App_Start中的页面循环并明确地将它们添加到RoutesTable.这工作正常,我们已经设置了足够长的AppPool刷新,使其成为一天一次的任务.这确实让我们为我们的用户提供了24小时的“为网页直播”的转变,我正试图解决这个问题.
理想情况下,我想做的是在用户注册后动态地将相关路由添加到RouteTable.我试过这样做:
RouteTable.Routes.Add( RouteTable.Routes.MapRoute( toAdd.UrlSlug + "Homepage",toAdd.UrlSlug,new { controller = "Controller",View = "View",urlSlug = toAdd.UrlSlug } ) );
解决方法
如果您尝试使用路径约束,该怎么办?获取所有用户的列表并约束所选路由以匹配该列表中的条目
public class UserPageConstraint : IRouteConstraint { public static IList<string> UserPageNames = (Container.ResolveShared<IUserService>()).GetUserPageNames(); bool _IsUserPage; public UserPageConstraint(bool IsUserPage) { _IsUserPage = IsUserPage; } public bool Match(HttpContextBase httpContext,Route route,string parameterName,RouteValueDictionary values,RouteDirection routeDirection) { if (_IsUserPage) return UserPageNames.Contains(values[parameterName].ToString().ToLower()); else return !UserPageNames.Contains(values[parameterName].ToString().ToLower()); } }
然后在Global.asax.cs中,为用户设置路由,如下所示:
routes.MapRoute("UserHome","{userPage}",new { controller = "UserPageController",action = "Index" },new { userPage = new UserPageConstraint(true) });
对于上面这个路由,在UserPageController的动作’index’中,我们将userPage作为参数.
对于相对于userPage Home的其他路由,我们可以相应地添加路由.例如,userdetails页面路由可以添加如下:
routes.MapRoute("UserHome","{userPage}/mydetails",action = "Details" },new { userPage = new UserPageConstraint(true) });
你可以试试看看.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。