1

i'm writing a code for making a site under maintenance.Can anyone help me please.the requirements are as follows.i have a project code i need to make that website go under maintenance from a specific time to specific time like example from 18 to before 8 it should be under maintenance and it should be done in global.asax file and start and end time should be like keys whose value is present webconfig file as shown below can any one please help me out?? code for global.aspx should be like dis...................

            // Clear the response stream 
            var httpContext = HttpContext.Current;
            httpContext.Response.Clear();
            httpContext.ClearError();
            httpContext.Response.TrySkipIisCustomErrors = true;


            var routeData = new RouteData();
            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("action", action);

            // Call error Controller and pass the routeData.
            using (var controller = new ErrorController())
            {
                ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
            }

the key should be as follows...

<add key="StartTime" value="8"/>
    <add key="EndTime" value="18"/> 

the logic i have taken is as follows

public class UnderMaintenanceAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var requestTimestamp = filterContext.HttpContext.Timestamp;
            if (IsUnderMaintenance(requestTimestamp))
            {
                filterContext.Result = new RedirectToRouteResult(
                    // create your controller/action/view to display your message
                    new RouteValueDictionary
                    {
                    { "controller", "Undermaintenance" },
                    { "action", "Index" }
                    });
            }
        }


        private bool IsUnderMaintenance(DateTime requestTimestamp)
        {
            bool isUnderMaintenance = requestTimestamp.Hour >= 18 || requestTimestamp.Hour < 13;

            return isUnderMaintenance;
        }
    }

but i have written that in route config my manager wants me to write it in global.aspx with start key and end key

Aakib13
  • 33
  • 4
  • can any help please – Aakib13 Jan 30 '20 at 10:00
  • just point your DNS to the maintenance page till the time you are done. – Sheelpriy Jan 30 '20 at 10:22
  • @sheelpriy no actually its a project and i need to be under maintenance every day after eve 6 to mng 8 and again it should get resumed after mng 8 till eve 6 – Aakib13 Jan 30 '20 at 11:09
  • can any one please help – Aakib13 Jan 30 '20 at 16:33
  • Use a global filter and register it conditionally using your constraints from the web.config. I am sure you can make something work from these answers: https://stackoverflow.com/questions/7580911/implement-down-for-maintenance-page – eaglei22 Feb 04 '20 at 20:37
  • @eaglei22 if i do that then its giving a error too many redirects – Aakib13 Feb 05 '20 at 07:48
  • @eaglei22 can u please tell here is the code https://stackoverflow.com/questions/60070557/localhost-redirected-you-too-many-times-in-mvc – Aakib13 Feb 05 '20 at 07:49
  • In your filter put a check to see if you're at the correct action method by seeing what was called, and only redirect if the maintenance action method wasn't requested. – eaglei22 Feb 06 '20 at 15:27
  • Also you'll need to register the filter and conditionally check your constraints from the web.config inside the filter to see when to redirect. Since Application_Start() should only run once when the application is first fired up. Rereading my earlier comment I mispoke – eaglei22 Feb 06 '20 at 18:05

0 Answers0