views:

675

answers:

4

Here's what I'm trying to do in my Global.asax.vb:

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 that Server.Transfer fails:

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

How do I fix this? Or, what's a better way for me to do this?

+2  A: 

You have to specifiy a virtual path, i.e. a path relative to the application base (i.e. no paths external to the app), so something like: Server.Transfer("error.html") or Server.Transfer("/error.html") or Server.Transfer("~/error.html")

JonoW
"~/error.html": Error executing child request for /error.html.
Zack Peterson
"/error.html": Error executing child request for /error.html.
Zack Peterson
"error.html": Error executing child request for error.html.
Zack Peterson
I just did the same tests, they all worked, except if I put in an incorrect filename, then I get "Error executing child request for /test_WRONG.html.". Are you sure that error.html exists in the root?
JonoW
It does not exist. It should route to the FriendlyError view of the Error controller.
Zack Peterson
+8  A: 

I just found this on Scott Hanselman's blog here called ELMAH which is an Error Logger/Handler that you can use without having to change your code. You might want to look into it, since it seems to work nicely with MVC.

Joseph
We use a modified version of ELMAH here on Stack Overflow - it's very good.
Jarrod Dixon
@Jarrod - What types of things have you guys included in your fork?
Gavin Miller
ELMAH rules - its more helpful than log4net was. IMHO.
MikeJ
+2  A: 

ELMAH is an excellent error handler for catching unhandled exceptions. It plugs seamlessly into your web application via HttpModules and has various options for notification and logging.

Features:

  • SQL, XML, SQLite, InMemory Logging
  • Email notification
  • RSS feed
  • Detailed! logging of exceptions
  • Easy integration
  • Error Signalling - signal the error handler of an error while "dieing nicely" for the user

And FYI, SO uses ELMAH, albeit a forked version. This is the best architectural explanation and setup tutorial

Gavin Miller
+1  A: 

By default, ASP.NET MVC applications have a view Shared/Error.aspx, inheriting from

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

If your controller uses the attribute [HandleError], all exceptions will continue to bubble until caught, and they will end up on that page.

I simply added an inline Page_Load (valid in this case since it's 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 it, the friendly "Sorry..." message. It definitely looks like ELMAH is more robust, but for my needs, this was sufficient.

Peter J