2010-03-24 68 views
0

这里是我的Application_OnError事件接收器在global.asax.vb:使用自定义的错误没有得到重定向到自定义错误页 - ASP.Net

Sub Application_OnError(ByVal sender As Object, ByVal e As EventArgs) 

    Dim innerMostException As Exception = getInnerMostException(Me.Context.Error) 

    If TypeOf innerMostException Is AccessDeniedException Then 

     Security.LogAccessDeniedOccurrence(DirectCast(innerMostException, AccessDeniedException)) 

     Dim fourOhThree As Integer = DirectCast(HttpStatusCode.Forbidden, Integer) 

     Throw New HttpException(fourOhThree, innerMostException.Message, innerMostException) 

    End If 

End Sub 

你会看到,如果我们完成了最里面的异常的类型AccessDeniedException异常我们抛出一个新的HTTPExcpetion与AKA“禁止”

下面是相关的web.config条目403状态码:

<customErrors defaultRedirect="~/Application/ServerError.aspx" mode="On"> 
     <error statusCode="403" redirect="~/Secure/AccessDenied.aspx" /> 
    </customErrors>  

所以我们正在期待是重定向到t他访问了AccessDenied.aspx页面。我们得到是重定向到ServerError.aspx页面。

我们也尝试过这样的:

Sub Application_OnError(ByVal sender As Object, ByVal e As EventArgs) 

    Dim innerMostException As Exception = getInnerMostException(Me.Context.Error) 

    If TypeOf innerMostException Is AccessDeniedException Then 

     Security.LogAccessDeniedOccurrence(DirectCast(innerMostException, AccessDeniedException)) 

     Context.Response.StatusCode = DirectCast(HttpStatusCode.Forbidden, Integer) 

    End If 

End Sub 

哪个unsuprisingly也不起作用。

任何想法我们做错了什么?

回答

0

Application_Error旨在赶上不是由您的应用程序处理的错误。当它发生火灾时,一个错误已经发生,一切都与这个错误有关。如果你从Application_Error中发现错误,你实际上是在说“我的错误处理程序有错误”。取而代之的只是Server.Transfer到适当的页面。如果你想保留所有重定向逻辑在web.config中你可以看到this article如何解析的customErrors部分揣摩出重定向到。

这一切说,我没有试过,但你可以尝试调用Server.ClearError()

  Dim Ex = Server.GetLastError() 
      Server.ClearError() 
      Throw New HttpException(System.Net.HttpStatusCode.Forbidden, Nothing, Ex) 

我不认为它会因为我上面所说的,但它值得一试说的工作。

+0

是啊,我想我有点隧道visioned有关通过web.config中的自定义错误部分使用重定向,并驳回只是在做一个Server.Transfer的。谢谢您的帮助。 – user129345 2010-03-25 10:20:42