By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,304 Members | 2,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,304 IT Pros & Developers. It's quick & easy.

Handle errors in global.asax

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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

"Jonas" <Jo***@nospam.pl> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #2

P: n/a
In which section of global.asax ?

--
Patrice

"Jonas" <Jo***@nospam.pl> a écrit dans le message de
news:ej**************@TK2MSFTNGP10.phx.gbl...
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

Nov 20 '05 #3

P: n/a
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
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
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

"Jonas" <Jo***@nospam.pl> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #4

P: n/a
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

"Patrice" <no****@nowhere.com> wrote in message
news:O4**************@TK2MSFTNGP14.phx.gbl...
In which section of global.asax ?

--
Patrice

"Jonas" <Jo***@nospam.pl> a écrit dans le message de
news:ej**************@TK2MSFTNGP10.phx.gbl...
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


Nov 20 '05 #5

P: n/a
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

"Jonas" <Jo***@nospam.pl> wrote in message
news:Ow**************@TK2MSFTNGP12.phx.gbl...
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
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
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

"Jonas" <Jo***@nospam.pl> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 20 '05 #6

P: n/a
Me too :-)

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

/Jonas

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2******************@TK2MSFTNGP10.phx.gbl...
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

"Jonas" <Jo***@nospam.pl> wrote in message
news:Ow**************@TK2MSFTNGP12.phx.gbl...
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
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:uU**************@TK2MSFTNGP09.phx.gbl...
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

"Jonas" <Jo***@nospam.pl> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
> 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
>
>



Nov 20 '05 #7

P: n/a
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...

--
Patrice

"Jonas" <Jo***@nospam.pl> a écrit dans le message de
news:%2***************@TK2MSFTNGP15.phx.gbl...
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

"Patrice" <no****@nowhere.com> wrote in message
news:O4**************@TK2MSFTNGP14.phx.gbl...
In which section of global.asax ?

--
Patrice

"Jonas" <Jo***@nospam.pl> a écrit dans le message de
news:ej**************@TK2MSFTNGP10.phx.gbl...
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



Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.