views:

26

answers:

1

I'm working through some Error stuff, and I've tried converting Richard Dingwall's example over to VB.NET. The problem is that I'm getting an error:

Type ResourceNotFoundException is undefined

'<AttributeUsage(AttributeTargets.[Class] Or AttributeTargets.Method, Inherited:=True, AllowMultiple:=False)> _'
Public NotInheritable Class HandleResourceNotFoundAttribute : Inherits FilterAttribute : Implements IExceptionFilter
    Public Property View() As String

    Public Sub New()
        View = "NotFound"
    End Sub

    Public Sub New(ByVal _view As String)
        View = _view
    End Sub

    Public Sub OnException(ByVal filterContext As ExceptionContext) Implements System.Web.Mvc.IExceptionFilter.OnException
        Dim controller As Controller = TryCast(filterContext.Controller, Controller)
        If controller Is Nothing OrElse filterContext.ExceptionHandled Then
            Return
        End If

        Dim exception As Exception = filterContext.Exception
        If exception Is Nothing Then
            Return
        End If

        ''# Action method exceptions will be wrapped in a
        ''# TargetInvocationException since they're invoked using 
        ''# reflection, so we have to unwrap it.
        If TypeOf exception Is TargetInvocationException Then
            exception = exception.InnerException
        End If

        ''# If this is not a ResourceNotFoundException error, ignore it.

        ''# ###############################
        ''# ###############################

        If Not (TypeOf exception Is ResourceNotFoundException) Then ''# ERROR HERE

        ''# ###############################
        ''# ###############################

            Return
        End If

        filterContext.Result = New ViewResult() With { _
         .TempData = controller.TempData, _
         .ViewName = View _
        }

        filterContext.ExceptionHandled = True
        filterContext.HttpContext.Response.Clear()
        filterContext.HttpContext.Response.StatusCode = 404
    End Sub
End Class
A: 

If you read into Richard Dingwall's post (which I assume is this one), you'll notice that the ResourceNotFound exception is a custom exception which he has created, and provided code for. You'll need to implement that exception class yourself in order for the compiler to recognize that class.

Ryan Brunner
It appears as though his custom implementation is simply to declare it and inherit "exception" am I missing something? `public class ResourceNotFoundException : Exception {}`
rockinthesixstring
Nope, you have it right. The reason you would have an 'empty' exception like this is so that you can distinguish between ResourceNotFound exceptions and other sorts of exceptions (if for instance you just threw Exception, your handler code could catch all sorts of exceptions that you didn't plan on handling)
Ryan Brunner