View Full Version : Confusion between global.asax setting and web.config
expectomas
06-09-2011, 08:39 PM
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
bruce
06-10-2011, 11:40 AM
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.
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
wisemx
09-11-2011, 04:23 AM
Scroll down to the global.asax App error section of this page:
http://msdn.microsoft.com/en-us/library/aa479319.aspx
On this page look at the use of HttpContext.Current.Response.Redirect:
http://forums.asp.net/p/1417139/3128537.aspx
Hope those help.
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.Ser ver.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
vBulletin® ©Jelsoft Enterprises Ltd.