开发者

How can I redirect a site based on date in ASP.NET MVC?

开发者 https://www.devze.com 2023-03-13 22:09 出处:网络
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.

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?


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.


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
        };
    }

}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消