Would like suggestions on quick and easy error handling for asp.net mvc web application.
-
the quickest and easiest way is: don't! ... but of course, this solution is not recommended... – Steven A. Lowe Aug 19 '09 at 04:01
4 Answers
ELMAH in conjunction with an attribute that extends HandleError and logs the errors through ELMAH. See this question/answer for some ideas on how to get the HandleError attribute to work with ELMAH. Dan Swatik also blogged about this with an implementation based on the accepted answer in that question.

- 1
- 1

- 524,688
- 99
- 697
- 795
You can still use the old Application_Error method in the 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'll want to turn on customErrors too...
<customErrors mode="RemoteOnly" defaultRedirect="~/error">
<error statusCode="403" redirect="~/error/forbidden" />
<error statusCode="404" redirect="~/error/notfound" />
<error statusCode="500" redirect="~/error" />
</customErrors>

- 24,118
- 10
- 92
- 107
I've just recently employed a very quick-n-dirty error reporting solution of my own that might give you some ideas...
Create a base controller class, lets call it MyBaseController
. Have all your controllers inherit from this if you like, though this isn't necessary if you only have one controller.
Then you can override its partial OnException
method and insert whatever kind of error reporting you might like, like to send yourself an email with the exception's ToString(). 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 to be helpful: http://blog.dantup.me.uk/2009/04/aspnet-mvc-handleerror-attribute-custom.html in learning about all of this...
Good luck!
-f!

- 4,258
- 2
- 24
- 27
-
P.S., you can achieve practically the same 'quick-n-dirty' results from an Attribute you apply on your controllers (you can subclass the intrinsic HandleErrorAttribute i.e., [HandleError]) with practically the same amount of code... – Funka Aug 19 '09 at 06:19