Hot Tip
LANGUAGES: C#
TECHNOLOGIES: Web.config | Custom Error Pages
Where'd the Error Come From?
Here's an easy tip to get the URL of the page that caused your custom error page to appear.
By Jeff Prosise
ASP.NET developers often use <error> elements in web.config to redirect to custom error pages when errors occur. A commonly asked question regarding custom error pages is, "When an error occurs, how can the custom error page obtain the URL of the page that produced the error?" You can't get it from Server.GetLastError or Request.Path, but you can get it from the query string included in the error page's URL. Here's an example, with PageNotFound.aspx as the custom error page and foo.aspx as the page that produced the error:
http://localhost/PageNotFound.aspx?aspxerrorpath=/foo.aspx
PageNotFound.aspx can therefore obtain the URL of the page that generated the error like this:
string url = Request["aspxerrorpath"];
If you also would like to retrieve the IP address from which the request originated, read it from Request.ServerVariables["REMOTE_ADDR"] or Request.UserHostAddress.
Jeff Prosise is author of several books, including Programming Microsoft .NET (Microsoft Press). He also is a co-founder of Wintellect (http://www.wintellect.com), a software consulting and education firm that specializes in .NET.