Application_Error 中的 Server.Transfer(“error_404.aspx”) 返回空白页
我在 global.asx 的 Application_Error 子中查找 HttpExceptions
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim ex As Exception = HttpContext.Current.Server.GetLastError()
If ex IsNot Nothing Then
If TypeOf (ex) Is HttpUnhandledException Then
If ex.InnerException Is Nothing Then
Server.Transfer("error.aspx", False)
End If
ex = ex.InnerException
End If
If TypeOf (ex) Is HttpException Then
Dim httpCode As Integer = CType(ex, HttpException).GetHttpCode()
If httpCode = 404 Then
Server.ClearError()
Server.Transfer("error_404.aspx", False)
End If
End If
End If
End Sub
我可以单步执行此代码并确认它确实命中了 Server.Transfer("error_404.aspx") 以及 error_404.aspx 的 Page_Load,但它显示的所有内容是一个空白页。
I look for HttpExceptions in the Application_Error sub of my global.asx
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Dim ex As Exception = HttpContext.Current.Server.GetLastError()
If ex IsNot Nothing Then
If TypeOf (ex) Is HttpUnhandledException Then
If ex.InnerException Is Nothing Then
Server.Transfer("error.aspx", False)
End If
ex = ex.InnerException
End If
If TypeOf (ex) Is HttpException Then
Dim httpCode As Integer = CType(ex, HttpException).GetHttpCode()
If httpCode = 404 Then
Server.ClearError()
Server.Transfer("error_404.aspx", False)
End If
End If
End If
End Sub
I can step through this code and confirm it does hit the Server.Transfer("error_404.aspx"), as well as the Page_Load of error_404.aspx, but all it shows is a blank page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否正在清除响应缓冲区?您不知道已经存在什么,因为您是在 Application_Error 包罗万象中执行此操作。 Server.Transfer 只是将新页面生成的任何内容附加到现有的响应上。显然这可能会产生一些问题。
Are you clearing the Response buffer? You have no clue what is already there, since you are doing this in the Application_Error catch-all. Server.Transfer just appends whatever the new page generates onto the existing Response. Obviously this could create some issues.
如果将
Server.Transfer
更改为Response.Redirect
是否有效? (您可能必须使用 global.asax 中的 HTTPContext.Current 前缀。)我不确定 Server.Transfer 在您正在做的事情的上下文中是一个好主意,因为您实际上是要求 IIS 将 global.asax URL 呈现给浏览器。
Does it work if you change the
Server.Transfer
to aResponse.Redirect
? (You may have to use the HTTPContext.Current prefix from where you are in global.asax.)I'm not sure that a Server.Transfer is a good idea in the context of what you're doing, since you're effectively asking IIS to render the global.asax URL to the browser.
我认为如果发生一些错误,Server.Transfer 将不会触发。
Server.Transfer 不是一个好方法。尝试使用 Response.Redirect。它应该有效。
如果有任何例外,维护状态是否有任何要求?如果没有,请使用 Response.Redirect。
I think if some error occurs, Server.Transfer won't fire.
Server.Transfer is not a good method for this. Try with Response.Redirect. It should work.
If you have any exception is there any requirement for maintaining the states? If not, go with
Response.Redirect
.