HttpRequest.UrlReferrer Property
.NET Framework 4
Gets information about the URL of the client's previous request that linked to the current URL.
Assembly: System.Web (in System.Web.dll)
| Exception | Condition |
|---|---|
| UriFormatException |
The HTTP Referer request header is malformed and cannot be converted to a Uri object. |
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Safer coding response
If you don't have a referring page (e.g. coming from external site) then the Request.UrlReferrer object will be null. So yes, you need to check that the object exists before using the PathAndQuery property to avoid a null reference exception. This is a general coding consideration and not specific to this object.
- 4/30/2012
- Joel Theophanes
Safer coding: Request.UrlReferrer
this is a precaution ... i'm not 100% sure that it's necessary.
[NullReferenceException: Object reference not set to an instance of an object.]
The above exception appeared to have occurred with this code:
if ((Request.UrlReferrer.PathAndQuery != null) && (Request.UrlReferrer.PathAndQuery != String.Empty))
removing the first occurrence of .PathAndQuery appears to have prevented the error:
if ((Request.UrlReferrer != null) && (Request.UrlReferrer.PathAndQuery != String.Empty))
i say "appears to have prevented the error" because imho the error should not have occurred in the first form; i could be wrong.
the complete c# code snippet follows, below.
regards ~~ gerry (lowry)
string guestReferrerUrl = "not detected"; //
string guestReferrerUrlAbsolutePath = "not detected"; //
string guestReferrerUrlQuery = "not detected"; //
if ((Request.UrlReferrer != null) && (Request.UrlReferrer.PathAndQuery != String.Empty))
{
guestReferrerUrl = Request.UrlReferrer.AbsoluteUri;
guestReferrerUrlAbsolutePath = Request.UrlReferrer.AbsolutePath;
guestReferrerUrlQuery = Request.UrlReferrer.Query;
}
[NullReferenceException: Object reference not set to an instance of an object.]
The above exception appeared to have occurred with this code:
if ((Request.UrlReferrer.PathAndQuery != null) && (Request.UrlReferrer.PathAndQuery != String.Empty))
removing the first occurrence of .PathAndQuery appears to have prevented the error:
if ((Request.UrlReferrer != null) && (Request.UrlReferrer.PathAndQuery != String.Empty))
i say "appears to have prevented the error" because imho the error should not have occurred in the first form; i could be wrong.
the complete c# code snippet follows, below.
regards ~~ gerry (lowry)
string guestReferrerUrl = "not detected"; //
string guestReferrerUrlAbsolutePath = "not detected"; //
string guestReferrerUrlQuery = "not detected"; //
if ((Request.UrlReferrer != null) && (Request.UrlReferrer.PathAndQuery != String.Empty))
{
guestReferrerUrl = Request.UrlReferrer.AbsoluteUri;
guestReferrerUrlAbsolutePath = Request.UrlReferrer.AbsolutePath;
guestReferrerUrlQuery = Request.UrlReferrer.Query;
}
- 3/28/2012
- gerry lowry