views:

133

answers:

1

In the event my project is deployed with an incorrect, incomplete or missing connection string i'd like an application startup check for db connectivity, and if there is none, direct all requests to a DatabaseConnectionError page of some sort, what are my options? I don't think there's a specific http error code that would fit this error, and I'm not currently using web.config to define a generic catch all for all errors.

I do have

    /// <summary>
    /// from http://www.davidjuth.com/asp-net-mvc-error-handler.aspx
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="args"></param>
    protected void Application_Error(object sender, EventArgs args)
    {
        Exception ex = Server.GetLastError();
        using (var crashFile = new System.IO.StreamWriter(Server.MapPath("~/App_Data/Crash_" + DateTime.UtcNow.ToString("yyyyMMdd") + ".log")))
            crashFile.WriteLine("<crash><time>" + DateTime.UtcNow.TimeOfDay.ToString() + "</time><url>" + HttpContext.Current.Request.Url + "</url><exception>" + ex.ToString() + "</exception></crash>");

    }

but it doesn't appear to allow me to respond to the request with a page.

+1  A: 

You can redirect to a custom error page in the Application_Error callback:

protected void Application_Error(object sender, EventArgs args)
{
    ...
    HttpApplication app = (HttpApplication)sender;
    HttpContext context = app.Context;
    context.Response.Redirect("~/error.html");
}

You could also use context.Server.Transfer and set some special http error code such as 500 if you don't want to redirect.

Darin Dimitrov