Confusion between global.asax setting and web.config

Discussion in 'ASP.NET / ASP.NET Core' started by expectomas, Jun 9, 2011.

  1. Good Day to everyone,

    I am kind of confused between the settings of global.asax and web.config.

    As I understand that by catching http-related exceptions in global.asax, I can use server.transfer(url) to 'redirect' to respective error page.

    In view of this, does the web.config error page configuration still necessary. i.e. if I were to 1) remove redirection from web.config error redirect or 2) set custom mode to 'off' i can still transfer users to an NotFound (404) error page.

    Thank You
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    If you have exception management in the global.asax which redirect to an error page, the ASP.NET customer error will not take effect as the error is technically captured.
     
  3. Bruce,

    Could you provide an example of the "exception management in the global.asax " ?

    I have been playing with a response.redirect and a server.transfer in the global.asax and they dont seem to be working at all.

    Thanks,

    Miro
     
  4. Thanks

    Thank you bruce,

    I read the microsoft link the other night but I was still having problems.

    The link you posted: http://forums.asp.net/p/1417139/3128537.aspx gave me the fix I need.

    Incase anyone else runs into this ....

    I originally had in my global.asax code like this:
    (example)
    Response.Clear()
    Response.Redirect("PageNotFound.aspx")
    Response.End()

    From the top link I changed it to be:

    HttpContext.Current.Server.ClearError()
    HttpContext.Current.Response.Redirect("~/PageNotFound.aspx")

    and that worked.

    ======

    A dumbed down full snippet for anyone who is having the same problems is like this:

    Global.asax

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when an unhandled error occurs
    Dim lastErrorWrapper As HttpException = CType(Server.GetLastError(), HttpException)

    HttpContext.Current.Server.ClearError()

    If lastErrorWrapper.GetHttpCode() = 404 Then
    HttpContext.Current.Response.Redirect("~/Error404.aspx")
    Else
    Dim lastError As Exception = lastErrorWrapper
    If lastErrorWrapper.InnerException IsNot Nothing Then
    lastError = lastErrorWrapper.InnerException
    End If

    Dim lastErrorTypeName As String = lastError.GetType().ToString()
    Dim lastErrorMessage As String = lastError.Message
    Dim lastErrorStackTrace As String = lastError.StackTrace

    CreateHTMLErrorFile(lastErrorTypeName, lastErrorMessage, lastErrorStackTrace)
    HttpContext.Current.Response.Redirect("~/ErrorCustom.aspx")
    End If
    End Sub

    Private Sub CreateHTMLErrorFile(ByVal lastErrorTypeName As String, ByVal lastErrorMessage As String, ByVal lastErrorStackTrace As String)
    Dim HTML As String = _
    String.Format( _
    "<html>" & vbCrLf & _
    " <body>" & vbCrLf & _
    " <h1>An Error Has Occurred!</h1>" & vbCrLf & _
    " <table cellpadding=""5"" cellspacing=""0"" border=""1"">" & vbCrLf & _
    " <tr>" & vbCrLf & _
    " <td style=""text-align: right;font-weight: bold"">URL:</td>" & vbCrLf & _
    " <td>{0}</td>" & vbCrLf & _
    " </tr>" & vbCrLf & _
    " <tr>" & vbCrLf & _
    " <td style=""text-align: right;font-weight: bold"">User:</td>" & vbCrLf & _
    " <td>{1}</td>" & vbCrLf & _
    " </tr>" & vbCrLf & _
    " <tr>" & vbCrLf & _
    " <td style=""text-align: right;font-weight: bold"">Exception Type:</td>" & vbCrLf & _
    " <td>{2}</td>" & vbCrLf & _
    " </tr>" & vbCrLf & _
    " <tr>" & vbCrLf & _
    " <td style=""text-align: right;font-weight: bold"">Message:</td>" & vbCrLf & _
    " <td>{3}</td>" & vbCrLf & _
    " </tr>" & vbCrLf & _
    " <tr>" & vbCrLf & _
    " <td style=""text-align: right;font-weight: bold"">Stack Trace:</td>" & vbCrLf & _
    " <td>{4}</td>" & vbCrLf & _
    " </tr> " & vbCrLf & _
    " </table>" & vbCrLf & _
    " </body>" & vbCrLf & _
    "</html>", _
    Request.RawUrl, _
    User.Identity.Name, _
    lastErrorTypeName, _
    lastErrorMessage, _
    lastErrorStackTrace.Replace(Environment.NewLine, "<br />"))

    Dim path As String = "~/_Database/ErrorLogs/" & DateTime.Now.ToString("yyyymmdd_hhmmss") & ".html"

    If Not File.exists(System.Web.HttpContext.Current.Server.MapPath(path)) Then
    File.Create(System.Web.HttpContext.Current.Server.MapPath(path)).Close()
    End If

    Using w As StreamWriter = File.AppendText(System.Web.HttpContext.Current.Server.MapPath(path))
    w.WriteLine(HTML)
    w.Flush()
    w.Close()
    End Using
    End Sub



    =====

    My only issue now is - to figure out if I can get the error above to also show the line numbers where the error occured.

    Many Thanks,

    Cheers'

    Miro
     

Share This Page