"customErrors" in "web.config"

Discussion in 'ASP .Net' started by jk, Jun 11, 2009.

  1. jk

    jk Guest

    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.
    jk, Jun 11, 2009
    #1
    1. Advertising

  2. jk

    jk Guest

    > 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).
    jk, Jun 11, 2009
    #2
    1. Advertising

  3. 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...




    Juan T. Llibre, asp.net MVP
    ¿ Estas probando VS 2010 y ASP.NET 4.0 ?
    Regístrate (gratis) en los Foros de VS 2010 y ASP.NET 4.0, en español
    http://asp.net.do/foros/forums/
    =====================================================
    "jk" <_nospam@_no_spam.com> wrote in message news:...
    >> 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).
    >
    Juan T. Llibre, Jun 12, 2009
    #3
  4. jk

    jk Guest

    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.
    jk, Jun 12, 2009
    #4
  5. 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




    Juan T. Llibre, asp.net MVP
    ¿ Estas probando VS 2010 y ASP.NET 4.0 ?
    Regístrate (gratis) en los Foros de VS 2010 y ASP.NET 4.0, en español
    http://asp.net.do/foros/forums/
    =====================================================
    "jk" <_nospam@_no_spam.com> wrote in message news:%...
    > 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.
    >
    Juan T. Llibre, Jun 12, 2009
    #5
  6. jk

    jk Guest

    > 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.

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


    I'll take a look.

    Thanks again (appreciated!).
    jk, Jun 12, 2009
    #6
  7. jk

    jk Guest

    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.
    jk, Jun 12, 2009
    #7
  8. jk

    jk Guest

    > 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.
    jk, Jun 12, 2009
    #8
  9. 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.



    Juan T. Llibre, asp.net MVP
    ¿ Estas probando VS 2010 y ASP.NET 4.0 ?
    Regístrate (gratis) en los Foros de VS 2010 y ASP.NET 4.0, en español
    http://asp.net.do/foros/forums/
    =====================================================
    "jk" <_nospam@_no_spam.com> wrote in message news:...
    >> 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.
    >
    Juan T. Llibre, Jun 12, 2009
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Smith

    Web.Config - customErrors

    John Smith, May 11, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    5,149
    John Smith
    May 12, 2004
  2. Dave

    CustomErrors in Web.Config

    Dave, May 21, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    466
    Curt_C [MVP]
    May 21, 2004
  3. Kelmen Wong

    web.config customErrors cached?

    Kelmen Wong, Jul 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    364
    Kelmen Wong
    Aug 2, 2004
  4. Ron Weldy

    Customerrors in web.config question

    Ron Weldy, Jan 18, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    9,734
    Razvan Petcu
    Jan 25, 2005
  5. Chumley Walrus

    customerrors tag in web.config

    Chumley Walrus, Jan 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    13,300
    Juan T. Llibre
    Jan 22, 2005
Loading...

Share This Page