473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Handle errors in global.asax

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
7 2328
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Russ | last post by:
Hello. My new dev machine is running XP Pro. In the past all equipment has only used Windows 2000. I have had a lot of problems getting my projects up and running on the new machine. The current...
4
by: James | last post by:
I have the Application_Error event method in the Global.asax setup to send me an email regarding errors. But, I am getting reports from users that a specific page is producing a runtime error...
8
by: Vishwanathan Raman | last post by:
Hi I have a declared a static DataSet object SOBJ in Global.asax.I also have a localy defined DataSet LSOBJ in Global.asax which I am storing in Application State.Is there any technical...
5
by: ad | last post by:
The Global.asax is code-inside with default. How to change Global.asax to code-behind?
2
by: msnews.microsoft.com | last post by:
I am currently trying to follow the Que Publishing exam guide to the Microsoft Exam 70-320. I followed the guide to a tee, but when I try to add a web reference to my server side soap extension it...
11
by: Ron | last post by:
I have a web project compiled with the new "Web Deployment Projects" plugin for VS2005. I'm deploying the web project to one assembly and with updateable option set to ON. When I'm running the...
8
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project...
25
by: JJ | last post by:
I only want to catch 404 errors at the application level (the rest are will be handled by the customerrors section of the web.config). How do I check for the error code in the Application_Error...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.