在global.asax中出错时无法执行server.transfer

use*_*716 5 .net server.transfer global-asax

在我的global.asax页面中,我有以下代码:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
     server.transfer("err.aspx")
End Sub
Run Code Online (Sandbox Code Playgroud)

它不起作用,我得到以下错误:对象引用未设置为对象的实例.

提前致谢

Eri*_*oen 10

我建议在.NET中使用内置的错误处理,只需使用Web.config:

<configuration>
  <system.web>
    <customErrors mode="On" defaultRedirect="err.aspx" redirectMode="responseRewrite">
    </customErrors>
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

responseRewrite将使它充当Server.Transfer.如果您想要重定向,请使用redirectMode="responseRedirect".

更多信息:

但是,如果您真的想在Global.asax中处理它,则应使用该sender对象:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
     Dim app As HttpApplication = CType(sender, HttpApplication)
     app.Server.Transfer("err.aspx")
End Sub
Run Code Online (Sandbox Code Playgroud)