How to register unhandled exceptions in ASP.NET MVC?

Here is what I am trying to do in my Global.asax.vb file:

Public Class MvcApplication Inherits System.Web.HttpApplication Shared Sub RegisterRoutes(ByVal routes As RouteCollection) routes.IgnoreRoute("{resource}.axd/{*pathInfo}") routes.MapRoute( _ "Error", _ "error.html", _ New With {.controller = "Error", _ .action = "FriendlyError"} _ ) ... 'other routes go here' ... End Sub Sub Application_Start() RegisterRoutes(RouteTable.Routes) End Sub Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ... 'code here logs the unhandled exception and sends an email alert' ... Server.Transfer("http://www.example.com/error.html") End Sub End Class 

But Server.Transfer problem fails:

Invalid path for child request 'http://www.example.com/error.html'. A virtual path is expected.

How to fix it? Or, what is the best way for me to do this?

+7
exception asp.net-mvc global-asax
May 01, '09 at 15:50
source share
4 answers

I just found this on Scott Hanselman's blog here called ELMAH , which is an error logger / error handler that can be used without changing the code. You might want to look into it, as it looks good with MVC.

+9
May 01 '09 at 15:57
source share

ELMAH is a great error handler for finding unhandled exceptions. It easily connects to your web application through HttpModules and has various options for notification and logging.

Features:

  • SQL, XML, SQLite, writing to InMemory
  • Email Notification
  • RSS feed
  • Detailed! exception logging
  • Easy integration
  • Error signal - the error handler signals an error when he "knows how to die" for the user.

And FYI, SO uses ELMAH, although a branched version. This is the best architectural explanation and customization tutorial.

+3
May 01 '09 at 16:04
source share

You must specify a virtual path, that is, a path relative to the application database (i.e. no external paths to the application), something like: Server.Transfer ("error.html") or Server.Transfer ("/error.html" ) or Server.Transfer ("~ / error.html")

+2
May 01 '09 at 15:54
source share

By default, ASP.NET MVC applications are of the form Shared / Error.aspx, which inherits from

 System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo> 

If your controller uses the [HandleError] attribute, all exceptions will continue to bubble until they are caught and they get to this page.

I just added the built-in Load_page (valid in this case, since this is the end of the line):

 <script runat="server"> Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs) MyExceptionHandlerService.LogException("exceptionsource", this.Model.Exception) End Sub </script> 

After that, the friendly message "Sorry ...". It definitely looks like ELMAH is more reliable, but that was enough for my needs.

+2
May 01 '09 at 16:20
source share



All Articles