Getting the HTTP Referrer in ASP.NET
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"];
Related videos on Youtube
Comments
-
Chuck Le Butt 6 monthsI'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.
-
Big McLargeHuge over 8 yearspossible duplicate of How do I get the referrer URL in an ASP.NET MVC action?
-
-
Chuck Le Butt about 12 yearsSo there wouldn't be any different if I used: HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] ? -
Διονυσια Αγαλιώτη about 12 yearsIn theory there's no difference, in practice I can't say for sure since a quick look with reflector shows that
UrlReferrerdoes a lot more than a simple call toServerVariables("HTTP_REFERER") -
Chuck Le Butt about 12 yearsI can tell you thatServerVariables["HTTP_REFERER"]returns a string, whereasRequest.UrlReferrerreturns a Uri. -
Sheepy over 8 yearsTo safeguard against null, you can say: string actual = "" + Request.UrlReferrer ?? "(default)"; // (default) can be empty string -
NightOwl888 over 8 yearsIt should be noted that this property will throw a System.UriFormatException if the referer HTTP header is malformed. -
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 over 7 yearsIt 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 about 7 yearsBe careful if you are usingRequest.UrlReferrerafter a server side postback. Of courseRequest.UrlReferrerwill 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 about 7 years...and when you post back for instance, you could do:Response.Redirect(ViewState["PreviousPageUrl"] != null ? ViewState["PreviousPageUrl"].ToString() : "SomeOtherPage.aspx"); -
Kiquenet over 6 yearsValidHttpContext.Current.Request.UrlReferrer? -
Rudey over 5 yearsNote that theRefererheader is spelled differently than theHTTP_REFERRERserver variable. -
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 siteIf 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 ifAnd 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 over 3 yearsYeah 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 over 2 yearsThis is for ApiController. That's what I needed though.