"customErrors" in "web.config"

J

jk

Hi there,

I've set the "customErrors" tag in my "web.config" file to point to my
"Errors.aspx" page. Now how can I retrieve the actual error in that page's
"Page_Load" method so I can log it . Thanks in advance.
 
J

jk

I'm pretty sure you can use Server.LastError.

Thanks but that returns null (I already tried it previously). It works if
you trap "Page_Error()" for a given page but there has to be a way of
getting the error on a "customErrors" page (if not then this technique is
useless for diagnostic purposes).
 
J

Juan T. Llibre

re:
!> there has to be a way of getting the error on a "customErrors" page
!> (if not then this technique is useless for diagnostic purposes).

customErrors.aspx :
-------------------------------

<html>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim errMessage As String = ""
Dim appException As System.Exception = Server.GetLastError()
If (TypeOf (appException) Is HttpException) Then
Dim checkException As HttpException = CType(appException, HttpException)
Select Case checkException.GetHttpCode
Case 400
errMessage &= "Bad request. The file size is too large."
Case 401
errMessage &= "You are not authorized to view this page."
Case 403
errMessage &= "You are not allowed to view that page."
Case 404
errMessage &= "The page you have requested can't be found."
Case 408
errMessage &= "The request has timed out."
Case 500
errMessage &= "The server can't fulfill your request."
Case Else
errMessage &= "The server has experienced an error."
End Select
Else
errMessage &= "The following error occurred<BR>" & appException.ToString
End If
ErrorMessage.Text = errMessage & "<BR>We're sorry for the inconvenience."
Server.ClearError()
End Sub
</script>

<body>
<hr>
<asp:label id="ErrorMessage" font-size="12" font-bold="true" runat=server/>
<hr>
<p>Return to <a href="www.yourserver.com">My Server's entry page</a>
</body>
</html>
---------------------

HTH...




===============
 
J

jk

Thanks but "Server.GetLastError()" is returning null as mentioned in my last
post. I'm now trying to catch it via "Application_Error()" in my
"Global.asax" file where I can log the error there instead and then let it
propagate to my "customErrors" page (which shouldn't be necessary but I
can't find anyway to get the error in my "customErrors" page). Honestly
though, this is causing no end of frustration like so many other things
since I started working with .NET. In addition to this problem, I want to
use a (C#) code-behind file for my "Global.asax" file. I had to manually add
this myself however (after some digging) since Visual Studio doesn't do it
for you (go figure - it does it for a Web Application but not a Web *Site*).
After adding my "Global.asax.cs" file though, I got an "undefined" error on
the "HttpApplication" derivative I defined in that file. This occurs while
trying to precompile my site. I see no reason for this error however and in
fact, it goes away if I simply move these files into "App_Code" (why?). I
can't use it from "App_Code" however since my app is precompiled and it
therefore doesn't use "App_Code" (ASP.NET complains at runtime if you try to
use "App_Code" for a pre-compiled app). I can't get "Global.asax" to
precompile however because of the "undefined" error problem. This is nuts.
I've literally wasted hundreds of hours on issues like this and I'm not a
programming novice (25 years on MSFT platforms). IMO, MSFT is making things
harder as the years go on and my strong support for them has been waning in
recent years. Anyway, sorry for ranting and thanks for your help.
 
J

Juan T. Llibre

re:
!> then let it propagate to my "customErrors" page

I don't think you can do it that way.

You need to transfer to the errors page in global.asax this way :

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

You can catch any error in the errors.aspx page if you transfer
to errors.aspx from global.asax's Sub Application_Error.

In the code I posted, I only filter for httpExceptions.
If you want to address other types of exceptions you'll need to handle those cases explicitly.

Take a hint for doing that from :
http://books.google.com/books?id=58...kuSlCQ&sa=X&oi=book_result&ct=result&resnum=1




===============
 
J

jk

then let it propagate to my "customErrors" page
I don't think you can do it that way.

I'm still an ASP.NET rookie but based on this ...

http://support.microsoft.com/kb/306355

... I had the impression that my "customErrors" page would kick in so long as
I don't invoke "Server.ClearError()" in my "Application_Error()" handler
(see the first few sentences under "How to use the Web.config file"). I have
yet to confirm this though since I'm still experimenting. I still can't
pre-compile "Global.asax" as per my last post however - I'm working on this
now.
You need to transfer to the errors page in global.asax this way :

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

Yes, I considered that as my backup plan.
You can catch any error in the errors.aspx page if you transfer
to errors.aspx from global.asax's Sub Application_Error.

Hopefully :)
In the code I posted, I only filter for httpExceptions.
If you want to address other types of exceptions you'll need to handle
those cases explicitly.

I (previously) tried this using a code-behind file however and
"Server.GetLastError()" returns null. It works in the "Page_Error" handler I
tested though, as would be expected. Maybe your embedded code will work but
I don't see why it shouldn't work using a code-behind file. It's effectively
the same for all intents and purposes. I will try it of course but I still
can't get "global.asax" pre-compiled. My "undefined" error mysteriously
disappeared after rebooting but now I get a "Could not load type 'Global'"
error. I'm now researching this and even found some info from you under
http://bytes.com/groups/net-asp/618972-could-not-load-type-global.. There's
no obvious reason why I should be getting this.

I'll take a look.

Thanks again (appreciated!).
 
J

jk

Ok, all is fixed and working now with one issue. "Server.GetLastError()" is
returning "HttpUnhandledException" instead of the exception my code
originally threw (the "InnerException" member is null). Any ideas how to get
the original error if possible? (or if not, what's the recommended procedure
for handling this). Note BTW that the MSFT article cited in my last post was
correct. The "customErrors" page will appear if "Server.ClearError()" isn't
called in "Application_Error()". Thanks again for your help.
 
J

jk

Ok, all is fixed and working now with one issue. "Server.GetLastError()"
is returning "HttpUnhandledException" instead of the exception my code
originally threw (the "InnerException" member is null). Any ideas how to
get the original error if possible?

Should have thought of this:

"Server.GetLastError().GetBaseException()"

All is now working. Your help was greatly appreciated. Thanks again.
 
J

Juan T. Llibre

re:
!> All is now working. Your help was greatly appreciated. Thanks again.

You're quite welcome.
Glad to see you're up and running again.



===============
 

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

Similar Threads

customErrors 1
customErrors 1
Web.Config - customErrors 6
customerrors tag doesnt works 2
<customErrors 3
encrypted connectionStrings web.config customErrors 2
customerrors tag in web.config 1
customErrors 0

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top