Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 441
user2726716 Avatar asked Aug 28 '13 19:08

user2726716


People also ask

How to handle Application error in global asax?

You can handle default errors at the application level either by modifying your application's configuration or by adding an Application_Error handler in the Global. asax file of your application. You can handle default errors and HTTP errors by adding a customErrors section to the Web.

How to handle Application error in global asax in mvc?

Setting HandleError Attribute as a Global Filter Any unhandled exception that takes place within the boundary of the MVC application will now be handled by this global error handler. To test this global handler, comment out the [HandleError] attribute from the action or the controller and then run the application.


1 Answers

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:

  • http://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/aspnet-error-handling
  • http://msdn.microsoft.com/en-us/library/system.web.configuration.customerrorssection.redirectmode.aspx

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
like image 146
Erik A. Brandstadmoen Avatar answered Dec 08 '22 02:12

Erik A. Brandstadmoen