Custom Errors Not Working

G

Guest

I posted this question about a week or so ago. I never got any replies so
please forgive me if you have already read this question.

I have the following in my web config file:

<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" />

Here is the code in the Error.aspx:

protected void Page_Load(object sender, EventArgs e)
{
Exception err = Server.GetLastError().GetBaseException();

this.lblMessage.Text = err.ToString();
}

If mode is set to RemoteOnly I still get the yellow screen of death.
If mode is set to Off I still get the yellow screen of death.
If mode is set to On I get the yellow screen of death that tells me to set
the mode to RemoteOnly or Off to see the actual error message.

Why the heck doesn't it direct to my Error.aspx page and show the error that
way?
 
J

Juan T. Llibre

re:
Why the heck doesn't it direct to my Error.aspx page and show the error that way?

Any unhandled exception is captured by Application_Error.

What does your Application_Error sub/void in global.asax have ?

You should have something like :

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Server.Transfer("Error.aspx")
End Sub



Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
G

Guest

The Application_Error sub/void in global.asax has nothing. I didn't think it
needed any code since the customErrors section has an attribute for the
defaultRedirect, what is the point in this if I still need to write code to
do it in the Application_Error method?

Examples i've seen from Microsoft did not elude to having to do that.

-Demetri
 
J

Juan T. Llibre

re:
what is the point in this if I still need to write code to
do it in the Application_Error method?
Examples i've seen from Microsoft did not elude to having to do that.

Hi, Demetri.

No offense, but you need to get your hands dirty with exception handling.
There's a lot more to this than you think there is.

There's plain Exceptions and there's Unhandled Exceptions.
They're handled via different methods.

Check out Peter Bromberg's excellent article on Unhandled Exceptions :
http://www.eggheadcafe.com/articles/20060305.asp

Also, check out this KB :
http://support.microsoft.com/?id=911816

....and, for good measure, see this Code Project article :
http://www.codeproject.com/aspnet/ASPNETExceptionHandling.asp




Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
K

Kevin Jones

Demetri,

the problem is that the error page is displayed by using re-direction,
that means that when you get into the page handler for the error page
the exception has disappeared. Calling

Exception ex = Server.GetLastError().GetBaseException()

itself raises a null reference exception, because Server.GetLastError()
returns null (put a breakpoint here and see for yourself).

The way to handle this (as somebody mentioned) is to add an
Application_Error handler and in there either: Server.Transfer to
Error.aspx; or save the exception in the session and then in error.aspx
retrieve the exception from the session.

HTH

Kevin Jones
 

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

Top