Joe
Joe

Reputation: 1139

Can Not Redirect from Application-BeginRequest at First Request using RewritePath

I want to redirect to a maintenance screen when we are inside the defined maintenance window (between Start DateTime and End DateTime)

inside my Global.asax.cs file:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var maintStart = Convert.ToDateTime(CommonUtilities.GetAppConfigCredential("MaintenanceStartDateTime"));
    var maintEnd = Convert.ToDateTime(CommonUtilities.GetAppConfigCredential("MaintenanceEndDateTime"));
    DateTime nw = DateTime.Now;

    if (maintStart < nw && nw < maintEnd)
    {
        HttpContext.Current.RewritePath("MaintenancePage");
    }
}

If I start up my application outside the maintenance window, then wait until the window start DateTime (or simply change the config), I get redirected to the maintenance screen on the next request. However, if I try to start up my application during the maintenance window, i get the following error:

Server Error in '/' Application.
Runtime Error
Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated. 

Not sure how to debug this, or what my next step should be.

EDIT:

If I start up my application inside the maintenance window, I need:

HttpContext.Current.RewritePath("Home/MaintenancePage");

to make it work properly.

If I start up my application outside the maintenance window, then wait until the maintenance window start time, I need:

HttpContext.Current.RewritePath("MaintenancePage");

to make it work properly.

EDIT2:

Forgot to mention, I have this:

public ActionResult MaintenancePage()
{
    return View();
}

in my HomeController.

And I forgot to mention that the Maintenance Page was in the Views/Home folder.

Upvotes: 1

Views: 809

Answers (1)

Joe
Joe

Reputation: 1139

In my Global.asax.cs file, in Application_BeginRequest, this is the correct statement, with the correct path:

HttpContext.Current.RewritePath("/Home/MaintenancePage");

Upvotes: 2

Related Questions