如何在 ASP.NET MVC 中根据日期重定向站点?

发布于 2024-11-16 03:07:52 字数 557 浏览 1 评论 0原文

我正在尝试找到根据当前日期将用户重定向到页面的最佳方法。我想要完成的正是下面的代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一曲爱恨情仇 2024-11-23 03:07:52

我会将该代码放入自定义 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.

〃温暖了心ぐ 2024-11-23 03:07:52

这是我用于类似需求的代码,其中包含一些附加功能以使测试更容易。它可以设置为全局过滤器,但我更喜欢将其单独应用于控制器/操作,以便在启动之前可以使用特定页面。

请注意,这会返回 ViewResult 而不是 RedirectResult - 这样原始 URL 就会得到保留,这意味着如果具有正确角色的人员从占位符页面登录,他们可以被重定向到他们最初请求的 URL。

public sealed class PreviewAuthAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        // todo: if site is live, show page 
        if (DataStore.Instance.Settings.Get("islive") == "True") return;

        // if request is from localhost or build server, show page
        if (filterContext.HttpContext.Request.IsLocal) return;
        if (filterContext.HttpContext.Request.UserHostAddress.StartsWith("192.168.0")) return;

        // if user has has beta role, show page
        if (filterContext.HttpContext.Request.IsAuthenticated && (filterContext.HttpContext.User.IsInRole("Beta"))) return;


        // site is not live and user does not have access - show placeholder

        filterContext.Result = new ViewResult()
        {                
            ViewName="Placeholder",
            ViewData = filterContext.Controller.ViewData,
            TempData = filterContext.Controller.TempData
        };
    }

}

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.

public sealed class PreviewAuthAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        // todo: if site is live, show page 
        if (DataStore.Instance.Settings.Get("islive") == "True") return;

        // if request is from localhost or build server, show page
        if (filterContext.HttpContext.Request.IsLocal) return;
        if (filterContext.HttpContext.Request.UserHostAddress.StartsWith("192.168.0")) return;

        // if user has has beta role, show page
        if (filterContext.HttpContext.Request.IsAuthenticated && (filterContext.HttpContext.User.IsInRole("Beta"))) return;


        // site is not live and user does not have access - show placeholder

        filterContext.Result = new ViewResult()
        {                
            ViewName="Placeholder",
            ViewData = filterContext.Controller.ViewData,
            TempData = filterContext.Controller.TempData
        };
    }

}
你丑哭了我 2024-11-23 03:07:52

我不会在 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.

梦里南柯 2024-11-23 03:07:52

您可以通过实现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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文