Hi,
What options do I have for error handling in ASP.NET MVC?
Do I call GetLastError on the error page and send out the email or log it?
Hi,
What options do I have for error handling in ASP.NET MVC?
Do I call GetLastError on the error page and send out the email or log it?
In your Global.asax you can implement something like the following:
protected void Application_Error()
{
Exception lastException = Server.GetLastError();
GetLogger().Fatal(lastException); // your custom loggin code
}
McvContrib has "Rescue" attributes -- an idea borrowed from Monorail. You can do like so:
[Rescue("default", AutoLocate = true)]
public class MyController : ConventionController
You then create rescue views based on convention like so:
Etc. When an unhandled exception occurs in a controller, the rescue filter will render the page with the same name as the exception type. If no exact match is found it renders Exception.aspx. (I think maybe it works up the inheritence hierarchy till if finds a match.)
These rescue views implement ViewPage<HandleErrorInfo> so in the view page you access the exception as ViewData.Model.Exception. This is the one place where I put code in the codebehind -- to log the exception -- because it makes for a nice application boundary.
If you don't want to use MvcContrib, you can study the code to implement your own solution.