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.
-
2possible duplicate of [How do I get the referrer URL in an ASP.NET MVC action?](http://stackoverflow.com/questions/1471188/how-do-i-get-the-referrer-url-in-an-asp-net-mvc-action) – Big McLargeHuge May 29 '14 at 15:10
-
Fro Asp.Net Core see [How can I get Url Referrer in ASP.NET Core MVC?](//stackoverflow.com/q/38772394) – Michael Freidgeim Sep 21 '17 at 09:05
10 Answers
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).

- 1,023,142
- 271
- 3,287
- 2,928
-
8It should be noted that this property will throw a System.UriFormatException if the referer HTTP header is malformed. – NightOwl888 Sep 05 '14 at 20:15
-
1@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? – Ravi Nov 04 '14 at 00:21
-
1It 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. – John Sep 04 '15 at 18:23
-
10Be 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();` – JonH Sep 23 '15 at 16:52
-
5...and when you post back for instance, you could do: `Response.Redirect(ViewState["PreviousPageUrl"] != null ? ViewState["PreviousPageUrl"].ToString() : "SomeOtherPage.aspx");` – JonH Sep 23 '15 at 16:53
-
-
@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" – GGSoft Aug 04 '17 at 17:27
-
Yeah remember Referer (sic) is often passed empty in headers which returns null here, so you better null check this thing! Request.UrlReferrer?.AbsoluteUri – Chris Moschini Aug 30 '19 at 18:13
Request.Headers["Referer"]
Explanation
The Request.UrlReferrer
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.UrlReferrer
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["Referer"]
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.

- 55,572
- 24
- 139
- 212
-
3Note that the `Referer` header is spelled differently than the `HTTP_REFERRER` server variable. – Rudey Jul 07 '17 at 07:21
Use the Request.UrlReferrer
property.
Underneath the scenes it is just checking the ServerVariables("HTTP_REFERER")
property.

- 36,600
- 15
- 168
- 198

- 32,564
- 38
- 174
- 263
-
2So there wouldn't be any different if I used: HttpContext.Current.Request.ServerVariables["HTTP_REFERER"] ? – Chuck Le Butt Nov 23 '10 at 16:31
-
2In 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")` – Diadistis Nov 23 '10 at 16:42
-
13I can tell you that `ServerVariables["HTTP_REFERER"]` returns a *string*, whereas `Request.UrlReferrer` returns a *Uri*. – Chuck Le Butt Nov 23 '10 at 19:48
Like this: HttpRequest.UrlReferrer Property
Uri myReferrer = Request.UrlReferrer;
string actual = myReferrer.ToString();
-
2To safeguard against null, you can say: string actual = "" + Request.UrlReferrer ?? "(default)"; // (default) can be empty string – Sheepy Apr 07 '14 at 10:31
I'm using .Net Core 2 mvc, this one work for me ( to get the previews page) :
HttpContext.Request.Headers["Referer"];

- 458
- 5
- 17
Since Google takes you to this post when searching for C# Web API Referrer
here's the deal: Web API
uses a different type of Request
from normal MVC Request
called HttpRequestMessage
which does not include UrlReferrer
. Since a normal Web API
request does not include this information, if you really need it, you must have your clients go out of their way to include it. Although you could make this be part of your API Object
, a better way is to use Headers
.
First, you can extend HttpRequestMessage
to provide a UrlReferrer()
method:
public static string UrlReferrer(this HttpRequestMessage request)
{
return request.Headers.Referrer == null ? "unknown" : request.Headers.Referrer.AbsoluteUri;
}
Then your clients need to set the Referrer Header
to their API Request
:
// Microsoft.AspNet.WebApi.Client
client.DefaultRequestHeaders.Referrer = new Uri(url);
And now the Web API Request
includes the referrer data which you can access like this from your Web API
:
Request.UrlReferrer();

- 28,927
- 17
- 154
- 183
Using .NET Core or .NET 5 I would recommend this:
httpContext.Request.Headers.TryGetValue("Referer", out var refererHeader)

- 62,132
- 37
- 328
- 418
Sometime you must to give all the link like this
System.Web.HttpContext.Current.Request.UrlReferrer.ToString();
(in option when "Current" not founded)

- 1,662
- 19
- 30
Belonging to other reply, I have added condition clause for getting null.
string ComingUrl = "";
if (Request.UrlReferrer != null)
{
ComingUrl = System.Web.HttpContext.Current.Request.UrlReferrer.ToString();
}
else
{
ComingUrl = "Direct"; // Your code
}

- 3,638
- 12
- 53
- 87