Why Does Server.GetLastError() Return NULL
If .NET Framework insist on not telling you what the exception was then you have to start checking Context.AllErrors.
I wanted to check what can be the reason to make GetLastError() method angry with myself and tried search engines to see what other people say about it. Most of the people keep writing that GetLastError method of Server object returns null if you want to call it inside a page’s load or in the error page that causes a redirection and makes you loose the Exception object.
Well, I really don’t try to implement a page specific error handling architecture. What I am doing is simply trying to access the last error within the Error event of the Context instance in my custom IHttpModule.
So if you are stacked with Server.GetLastError, try using to access them as shown in the following code.
System.Text.StringBuilder sb = new System.Text.StringBuilder(); if (app.Context.AllErrors.Length > 0) { for (int i = 0; i < app.Context.AllErrors.Length; i++) { sb.Append(string.Format("\n\napp.Context.AllErrors[{0}]:\n\n{1}", i, app.Context.AllErrors[i])); } }
PS: "app" is considered to be the current HttpApplication object.
Update: I've just realized that EpiServer's built-in error handling module clears the exception so I can't access it using Server.GetLastError() method. Though it is still interesting how I can access the exception by walking through the Context.AllErrors collection. When I turned off EpiServer's built-in error handling feature, I started to get the exception using Server.GetLastError() but keep on mind that Context.AllErrors collection still works pretty fine.
I also posted this question on forums.asp.net, but I did not get any response.
http://forums.asp.net/p/1204058/2103097.aspx
Believe me or not, I have even red your post on the asp.net forums. I hope that can give you the imagination about how many posts I’ve checked :)