Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How to retrieve Http Error Code in Application_Error in file Global.asax.cs

How to retrieve Http Error Code in Application_Error in file Global.asax.cs

Scheduled Pinned Locked Moved ASP.NET
helpsysadmintutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FlorentBzh
    wrote on last edited by
    #1

    Hello, I would like to receive an e-mail when an Error Occurs on my WebSite but not for errors like "401 402 404...". So before to send it I test errors. This is my code: public class Global : System.Web.HttpApplication { protected void Application_Error(Object sender, EventArgs e) { System.Web.HttpException MyHttpError; MyHttpError = System.Web.HttpException.CreateFromLastError(Server.GetLastError().Message) ; if(!(MyHttpError.GetHttpCode()==404)) { //Send an email and redirect } else // just redirect } } The problem is that in my Spy Window the value of Server.GetLastError()._httpCode is 404 but the value of httpErreur.GetHttpCode() is 500 !! Please explain me or give me another solution. -- modified at 10:02 Friday 20th January, 2006

    M 1 Reply Last reply
    0
    • F FlorentBzh

      Hello, I would like to receive an e-mail when an Error Occurs on my WebSite but not for errors like "401 402 404...". So before to send it I test errors. This is my code: public class Global : System.Web.HttpApplication { protected void Application_Error(Object sender, EventArgs e) { System.Web.HttpException MyHttpError; MyHttpError = System.Web.HttpException.CreateFromLastError(Server.GetLastError().Message) ; if(!(MyHttpError.GetHttpCode()==404)) { //Send an email and redirect } else // just redirect } } The problem is that in my Spy Window the value of Server.GetLastError()._httpCode is 404 but the value of httpErreur.GetHttpCode() is 500 !! Please explain me or give me another solution. -- modified at 10:02 Friday 20th January, 2006

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, That's because the method CreateFromLastError returns a new HttpException, and it does not assign the httpcode of the last error to the new HttpException. Instead, it simply sets the HResult value from the last error by calling the HResultFromLastError method. So when the GetHttpCode method of this HttpException is called, it simply returns the default error code 500. If you want to create an HttpException wrapping the last error, you simply create an HttpException object with the new keyword and pass the last exception in as the internal exception of the new HttpException.

      F 1 Reply Last reply
      0
      • M minhpc_bk

        Hi there, That's because the method CreateFromLastError returns a new HttpException, and it does not assign the httpcode of the last error to the new HttpException. Instead, it simply sets the HResult value from the last error by calling the HResultFromLastError method. So when the GetHttpCode method of this HttpException is called, it simply returns the default error code 500. If you want to create an HttpException wrapping the last error, you simply create an HttpException object with the new keyword and pass the last exception in as the internal exception of the new HttpException.

        F Offline
        F Offline
        FlorentBzh
        wrote on last edited by
        #3

        Ok, thanks I simply do : System.Web.HttpException httpErreur =(HttpException)Server.GetLastError(); if(!(httpErreur.GetHttpCode()==404)) { } else { } and it works

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups