如何在 ASP.NET MVC 中根据日期重定向站点?
我正在尝试找到根据当前日期将用户重定向到页面的最佳方法。我想要完成的正是下面的代码。
DateTime Today = DateTime.Now;
DateTime LaunchDate = DateTime.Parse("17/06/11");
DateTime CloseDate = DateTime.Parse("19/06/11");
int isClosed = DateTime.Compare(CloseDate, Today);
int isOpen = DateTime.Compare(LaunchDate, Today);
if (isClosed < 0){
return RedirectToAction("Closed", "Home");
}
else if (isOpen > 0){
return RedirectToAction("Index", "Home");
}
else{
return RedirectToAction("ComingSoon", "Home");
}
这种情况会在 global.asax 中的什么位置(或者甚至可能)发生?
I'm trying to find the best way to redirect a user to a page based on the current date. Exactly What I'm trying to accomplish is in the code below.
DateTime Today = DateTime.Now;
DateTime LaunchDate = DateTime.Parse("17/06/11");
DateTime CloseDate = DateTime.Parse("19/06/11");
int isClosed = DateTime.Compare(CloseDate, Today);
int isOpen = DateTime.Compare(LaunchDate, Today);
if (isClosed < 0){
return RedirectToAction("Closed", "Home");
}
else if (isOpen > 0){
return RedirectToAction("Index", "Home");
}
else{
return RedirectToAction("ComingSoon", "Home");
}
Where in the global.asax(or is it even possible) would this condition go?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会将该代码放入自定义 MvcHandler 中。
您可以将其放入 ActionFilter 中,但随后您必须将其应用于所有操作。
I would put that code in a custom MvcHandler.
You could put it in ActionFilter, but then you would have to apply it to all actions.
这是我用于类似需求的代码,其中包含一些附加功能以使测试更容易。它可以设置为全局过滤器,但我更喜欢将其单独应用于控制器/操作,以便在启动之前可以使用特定页面。
请注意,这会返回 ViewResult 而不是 RedirectResult - 这样原始 URL 就会得到保留,这意味着如果具有正确角色的人员从占位符页面登录,他们可以被重定向到他们最初请求的 URL。
Here's the code I use for a similar requirement, with a couple of additional features to make testing easier. It could be set up as a global filter, though I prefer to apply it to controllers/actions individually so that specific pages can be available before launch.
Note that this returns a ViewResult rather than a RedirectResult - this way the original URL is maintained, which means that if someone with the right role logs in from the placeholder page they can be redirected to the URL they originally requested.
我不会在 global.asax 中执行此操作,尽管您可以对其进行设置以找出您已注册的路线。假设整个“站点”即将推出,开放和关闭。这种方法的问题是,如果它适用于您的情况,那么有人可以通过玩弄来规避。哎哟!
当我打字时,Jakub 弹出了 Handler,这是一个不错的选择。您可以对其进行设置,这样除了您想要的页面之外,就看不到任何页面,这听起来就像您想要的那样。
I would not do this in global.asax, although you could set this up to figure out the routes you have registered. Assumes that the entire "site" will be coming soon, open and closed. The problem with this method, if it works in your case, is someone can circumvent by playing around. Ouch!
As I was typing, Jakub popped up with Handler, which is a good option. You can set it so no pages can be seen other than the one you desire, which is what it sounds like you want.
您可以通过实现
IRouteConstraint
来创建自定义RouteConstraint。在 match 方法中,您可以添加要检查的日期时间的逻辑。这需要您的路由表中有多个路由。它们都引用不同的控制器/操作,您可以在其中向用户显示不同的视图。下面的 url 显示了许多有关如何实现自定义 RouteConstraint 的示例。
http ://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx
You could create custom RouteConstraints by implementing the
IRouteConstraint
. Within the match method you could add the logic for the datetimes you want to check. This would require you to have multiple routes in your routetable. which all refer to different controllers/actions where you can show a different View to the user.Below url shows lots of samples on how to implement a custom RouteConstraint.
http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx