Getting the HTTP Referrer in ASP.NET

205,986

Solution 1

You could use the UrlReferrer property of the current request:

Request.UrlReferrer

This will read the Referer HTTP header from the request which may or may not be supplied by the client (user agent).

Solution 2

Request.Headers["Referer"]

Explanation

The Request.UrlReferer property will throw a System.UriFormatException if the referer HTTP header is malformed (which can happen since it is not usually under your control).

Therefore, the Request.UrlReferer property is not 100% reliable - it may contain data that cannot be parsed into a Uri class. To ensure the value is always readable, use Request.Headers["Referrer"] instead.

As for using Request.ServerVariables as others here have suggested, per MSDN:

Request.ServerVariables Collection

The ServerVariables collection retrieves the values of predetermined environment variables and request header information.

Request.Headers Property

Gets a collection of HTTP headers.

Request.Headers is a better choice than Request.ServerVariables, since Request.ServerVariables contains all of the environment variables as well as the headers, where Request.Headers is a much shorter list that only contains the headers.

So the most reliable solution is to use the Request.Headers collection to read the value directly. Do heed Microsoft's warnings about HTML encoding the value if you are going to display it on a form, though.

Solution 3

Use the Request.UrlReferrer property.

Underneath the scenes it is just checking the ServerVariables("HTTP_REFERER") property.

Solution 4

Like this: HttpRequest.UrlReferrer Property

Uri myReferrer = Request.UrlReferrer;
string actual = myReferrer.ToString();

Solution 5

I'm using .Net Core 2 mvc, this one work for me ( to get the previews page) :

HttpContext.Request.Headers["Referer"];
Share:
205,986

Related videos on Youtube

Chuck Le Butt
Author by

Chuck Le Butt

Hello.

Updated on July 08, 2022

Comments

  • Chuck Le Butt
    Chuck Le Butt 6 months

    I'm looking for a quick, easy and reliable way of getting the browser's HTTP Referrer in ASP.Net (C#). I know the HTTP Referrer itself is unreliable, but I do want a reliable way of getting the referrer if it is present.

  • Chuck Le Butt
    Chuck Le Butt about 12 years
    So there wouldn't be any different if I used: HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] ?
  • Διονυσια Αγαλιώτη about 12 years
    In theory there's no difference, in practice I can't say for sure since a quick look with reflector shows that UrlReferrer does a lot more than a simple call to ServerVariables("HTTP_REFERER")
  • Chuck Le Butt
    Chuck Le Butt about 12 years
    I can tell you that ServerVariables["HTTP_REFERER"] returns a string, whereas Request.UrlReferrer returns a Uri.
  • Sheepy
    Sheepy over 8 years
    To safeguard against null, you can say: string actual = "" + Request.UrlReferrer ?? "(default)"; // (default) can be empty string
  • NightOwl888
    NightOwl888 over 8 years
    It should be noted that this property will throw a System.UriFormatException if the referer HTTP header is malformed.
  • Ravi
    Ravi about 8 years
    @Darin Dimitrov Am trying to create a REST API using WEB API. UrlReferrer is not part of the Request object. Should i add some "using" etc. What am I missing? a DLL?
  • John
    John over 7 years
    It should be noted that the Difference is spellings is correct. The http header is misspelled. MS uses the correct spelling in the property name. Unfortunately, the two do not match, which can cause some people (me) confusion when testing.
  • LoganS
    LoganS about 7 years
    Be careful if you are using Request.UrlReferrer after a server side postback. Of course Request.UrlReferrer will now have the value of the page you are posting back to. In most cases, people need the previous page. In this case, ensure you are storing the previous page in say a viewstate variable when the page first loads. And then when you access this variable it has the previous page you came from. For example, in asp.net forms page load event you can do: if (Request.UrlReferrer != null) ViewState["PreviousPageUrl"] = Request.UrlReferrer.ToString();
  • LoganS
    LoganS about 7 years
    ...and when you post back for instance, you could do: Response.Redirect(ViewState["PreviousPageUrl"] != null ? ViewState["PreviousPageUrl"].ToString() : "SomeOtherPage.aspx");
  • Kiquenet
    Kiquenet over 6 years
    Valid HttpContext.Current.Request.UrlReferrer?
  • Rudey
    Rudey over 5 years
    Note that the Referer header is spelled differently than the HTTP_REFERRER server variable.
  • GGSoft
    GGSoft over 5 years
    @darin dimitrov The following code for my asp.net site returns null, when I try to open site from link on another site If Not IsPostBack Then Dim referrer As Uri = HttpContext.Current.Request.UrlReferrer If referrer Is Nothing Then Response.Write("Null") Else Response.Write(referrer.ToString) end if end if And it writes "Null" every time. whats wrong? I have changed page from where link comes and now works PHP script header(location: mysiteurl') but it still writs "Null"
  • Chris Moschini
    Chris Moschini over 3 years
    Yeah remember Referer (sic) is often passed empty in headers which returns null here, so you better null check this thing! Request.UrlReferrer?.AbsoluteUri
  • Csaba Toth
    Csaba Toth over 2 years
    This is for ApiController. That's what I needed though.

Related