我正在处理一些错误的东西,我尝试将Richard Dingwall的例子转换为VB.NET。问题是我收到了一个错误:
类型ResourceNotFoundException未定义
'<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
答案 0 :(得分:0)
如果您阅读Richard Dingwall的帖子(我假设是this one),您会注意到ResourceNotFound异常是他创建的自定义异常,并提供了代码。您需要自己实现该异常类,以便编译器识别该类。