Skip navigation

Where’d the Error Come From? - 30 Oct 2009

Here’s an easy tip to get the URL of the page that caused your custom error page to appear.

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.

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish