On error in global.asax can't do server.transfer
Asked Answered
L

1

5

In my global.asax page I have the following code:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
     server.transfer("err.aspx")
End Sub

It does not work and I get the folowing error: Object reference not set to an instance of an object.

Thanks in advance

Librate answered 28/8, 2013 at 19:12 Comment(0)
W
11

I would recommend using the built-in error handling in .NET for this, just use Web.config:

<configuration>
  <system.web>
    <customErrors mode="On" defaultRedirect="err.aspx" redirectMode="responseRewrite">
    </customErrors>
  </system.web>
</configuration>

The responseRewrite will make it act as a Server.Transfer. If you want a redirect instead, use redirectMode="responseRedirect".

More info here:

However, if you really want to handle it in Global.asax, you should use the sender object:

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
Wrasse answered 28/8, 2013 at 19:29 Comment(1)
Hi,Thanks for the answer. I tried Dim server As HttpApplication = CType(sender, HttpApplication) server.Transfer("err.aspx") as you mentioned but i get error: transfer is not a member of httpaplicationLibrate

© 2022 - 2024 — McMap. All rights reserved.