2014-04-24 29 views
0

我有在代码中实现这样的自定义错误处理程序:为什么我的自定义错误处理工作不正常?

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) 
    'get the exception 
    Dim ex = Server.GetLastError() 

    'log it 
    ex.LogToFile() 

    'clear the error 
    Server.ClearError() 

    'build route data to route this exception to ErrorController 
    Dim routeData = New RouteData() 
    routeData.Values.Add("controller", "Error") 
    routeData.Values.Add("action", "Index") 
    routeData.Values.Add("exception", ex) 

    'route to the error controller, preserving the context 
    Dim controller As IController = New Controllers.ErrorController() 
    controller.Execute(New RequestContext(New HttpContextWrapper(Context), routeData)) 
End Sub 

然后/error/index操作方法在一个用户友好的方式除外。在我的开发机器上,这工作正常。但是在部署的服务器上,当引发异常而不是我的错误消息时,它会尝试使用预设的错误页面,如%SystemDrive%\inetpub\custerr\<LANGUAGE-TAG>\500.htm

如果我指定了我自己的500页,它将使用它,但是没有我的自定义处理。

如果我删除了500页,像这样:

<httpErrors> 
    <remove statusCode="500" subStatusCode="-1" /> 
    </httpErrors> 

我只是得到死亡的白色屏幕此消息:

The page cannot be displayed because an internal server error has occurred. 

那么,怎样才能让我的错误处理做正确的事情?

回答

0

我有一种感觉,HandleErrorAttribute是造成这种情况。检查Global.asax和App_Start/FilterConfig.vb以查看是否存在类似filters.Add(New HandleErrorAttribute())的行。

如果使用HandleErrorAttribute并且自定义错误被打开,MVC将在MVC工作流程中保留所有未处理的异常。意味着来自views/controllers/etc的任何未处理的异常不会冒泡到ASP.NET或IIS以允许Application_Error看到异常,并且您将在/Views/Shared/Errors.vbhtml上看到视图

Looking在你的代码中,你几乎在做什么HandleErrorAttribute。如果您只是试图返回失败时的简单视图,则该属性确实允许您设置它使用的默认视图,而不是通用的错误视图。

相关问题