A minimalist approach to error handling

Wishes for fast and easy error handling for asp.net mvc web application.

+2


source to share


4 answers


ELMAH combined with an attribute that extends HandleError and logs errors via ELMAH. See this question / answer for some ideas on how to get the HandleError attribute to work with ELMAH. Dan Swatik also blog about this with an implementation based on the accepted answer in this question.



+4


source


You can still use the old Application_Error method in Global.asax

protected void Application_Error(object sender, EventArgs e)
{
    // some good info in Server.GetLastError().GetBaseException() and Context.Request
    // that you can use in whatever you choose for 
    // your quick and easy logging/reporting
}

      



Obviously, you also want to include customErrors ...

<customErrors mode="RemoteOnly" defaultRedirect="~/error">
  <error statusCode="403" redirect="~/error/forbidden" />
  <error statusCode="404" redirect="~/error/notfound" />
  <error statusCode="500" redirect="~/error" />
</customErrors>

      

+2


source


Check out the Nerd Dinner sample. It takes a very simple and straightforward approach.

+1


source


I recently used a very quick-dirty error message that might give you some ideas ...

Create a base controller class, call it MyBaseController

. You have all of your controllers if you like it, although this is not necessary if you only have one controller.

Then you can override its partial method OnException

and insert any error messages you might like, such as sending you an email with a ToString () exception. For example:

public MyOwnBaseController : Controller
    protected override void OnException(ExceptionContext context)
    {
        SendAdminErrorAlert("Egads! An Error!", context.Exception.ToString());
        // I have a view named "500.aspx" that I will now show...
        context.ExceptionHandled = true;
        this.View("500").ExecuteResult(this.ControllerContext);
    }
}

      

I also found this article helpful: http://blog.dantup.me.uk/2009/04/aspnet-mvc-handleerror-attribute-custom.html after learning about it all ...

Good luck!
-f

+1


source







All Articles