views:

182

answers:

1

I know that ASP.NET MVC has error filter attribute to handle specified error type. However, this feature cannot catch any error that occurs when application start. Therefore, I need to add some code to “Application_Error” method for handling this error like the following code.

public void Application_Error(object sender, EventArgs e)
{
    // At this point we have information about the error
    var ctx = HttpContext.Current;

    var exception = ctx.Server.GetLastError();

    var errorInfo =
       "<br>Offending URL: " + ctx.Request.Url +
       "<br>Source: " + exception.Source +
       "<br>Message: " + exception.Message +
       "<br>Stack trace: " + exception.StackTrace;

    ctx.Response.Write(errorInfo);

    Server.ClearError();
}

Although, this code will works fine, when normal application error occurs like error that occurs in view page. Nevertheless, it does not work when error occurs on application starting because request and response objects are always null.

Next, I try to solve this question by setting default redirect in custom errors like the following code.

<customErrors mode="On" defaultRedirect="Scripts/ApplicationError.htm"></customErrors>

Unfortunately, it does not work because when application receive redirected request, it try to start application again and it throw exception again.

How do to solve this problem? Alternatively, Do you have other idea for handling this error.

Thanks,

PS. The main reason for creating this handler because I want to display error when application cannot connect to other service like database for caching data on application start.

+1  A: 

The reason why Request and Response objects are null inside your Application_Error event is because the Application_Start event is not associated with any user, therefore you cannot redirect from it. Only the Application and Server built-in objects are available. Referencing the Session, Request, or Response objects would cause an error.

One workaround is to use try/catch and in the catch block log the error and set some global variable which indicates that something went wrong during application startup. Later on Application_BeginRequest you could check this variable and redirect accordingly.

Darin Dimitrov
I use to create some code that similar with your answer. I clear any error when application error is fired and save last error to public static variable. After that, I have some action filter that check this variable and display error if it available. However, I do not like this idea. Because it is possible to show error page to other request that, cause this error.
Soul_Master
There shouldn't be any other requests. If your application fails at initialization I don't see how it would handle any requests if it is in a failed state. The only workaround would be to fix the problem and restart the application.
Darin Dimitrov
I agree that subsequent requests should not occur if a startup error occurs, so you need a try/catch around all of Application_Start. If you check for your startup exception in BeginRequest, is there a more MVC friendly way to redirect than Response.Redirect? Mine redirects in a way that Firefox says will not end. Is there some way to redirect to an action that takes the exception, like Error.Startup(ex).
flipdoubt