Handle errors in global.asax

J

Jonas

Hi!

I have an Application_Error method in global.asax that uses Server.Transfer
to move execution to a custom error page. This works fine when an exception
is thrown in one of the aspx or ascx files. The errorpage.aspx uses
Server.GetLastError() and shows a friendly error page.

But when it happens in global.asax itself, the Server.Transfer does not
work. Do I have to use some kind of special exception handling for exception
in global.asax?

TIA

Jonas

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

' Fires when an error occurs

' Redirect to custom errorpage, Transfer must be used to get the
exception in the called page.

Context.Server.Transfer("~/errorpage.aspx")

End Sub
 
K

Kevin Spencer

You can log the exception if you wish. If the context of the exception
occurs outside of the context of a Request, there can be no redirect
involved. Of course, this means that there is no client at the other end
either, so it really makes no difference to anyone but you and whomever
maintains the site. So logging is a good idea.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
 
J

Jonas

Hi Kevin,

As I have centralized the code for logging and display the error in the
custom aspx, I would like to transfer/redirect to that page.
What happens is that I check if the user is authenticated in
Application_AuthenticateRequest, and if not authenticated, I want to throw
and ApplicationException and go to the error page and display this.

Instead, the user gets the default ASP.NET error page with my custom
exception message. Is there anyway around this?

TIA

Jonas

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

If Not User.Identity.IsAuthenticated Then

Dim exp As New ApplicationException("Authentication error")

exp.Source = "Internet Information Services"

Throw exp

End If

End Sub
 
J

Jonas

Hi,

It is in Application_AuthenticateRequest, see below.

Jonas

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

If Not User.Identity.IsAuthenticated Then

Dim exp As New ApplicationException("Authentication error")

exp.Source = "Internet Information Services"

Throw exp

End If

End Sub
 
K

Kevin Spencer

I'm stumped Jonas. Perhaps as a workaround you could do a transfer or
redirect from within the method instead of throwing the error.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
If you push something hard enough,
it will fall over.
- Fudd's First Law of Opposition
 
J

Jonas

Me too :)

I also tried a simple transfer/redirect without throwing an exception, but
then I got an "Error executing child request for ..."

/Jonas
 
P

Patrice

And what if you try to Server.Transfer from here ? Also could it be that the
target page is not allowed for unauthenticated users (how ASP?NET would
react ? I don't know)... Start perhaps by adding a small trace to see if
it's just the Server.Transfer that fails...

As a side note, it is likely already taken care by ASP.NET. Have you tried
to configure the web.config to direct this error to a custom page ? This
would be likely my prefered method...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top