Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


// Microsoft.AspNet.WebApi.Client
client.DefaultRequestHeaders.Referrer = new Uri(url);
API Request
HttpRequestMessage
Referrer Header
Request.UrlReferrer();
UrlReferrer()
public static string UrlReferrer(this HttpRequestMessage request)
{
    return request.Headers.Referrer == null ? "unknown" : request.Headers.Referrer.AbsoluteUri;
}

And now the Web API Request includes the referrer data which you can access like this from your Web API:

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.

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


Response.Redirect(ViewState["PreviousPageUrl"] != null                         ? ViewState["PreviousPageUrl"].ToString()                         : "SomeOtherPage.aspx");

@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?

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();

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.

It should be noted that this property will throw a System.UriFormatException if the referer HTTP header is malformed.

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


// Microsoft.AspNet.WebApi.Client
client.DefaultRequestHeaders.Referrer = new Uri(url);
API Request
HttpRequestMessage
Referrer Header
Request.UrlReferrer();
UrlReferrer()
public static string UrlReferrer(this HttpRequestMessage request)
{
    return request.Headers.Referrer == null ? "unknown" : request.Headers.Referrer.AbsoluteUri;
}

And now the Web API Request includes the referrer data which you can access like this from your Web API:

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.

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


Response.Redirect(ViewState["PreviousPageUrl"] != null                         ? ViewState["PreviousPageUrl"].ToString()                         : "SomeOtherPage.aspx");

@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?

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();

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.

It should be noted that this property will throw a System.UriFormatException if the referer HTTP header is malformed.

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


Response.Redirect(ViewState["PreviousPageUrl"] != null                         ? ViewState["PreviousPageUrl"].ToString()                         : "SomeOtherPage.aspx");

@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?

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();

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.

It should be noted that this property will throw a System.UriFormatException if the referer HTTP header is malformed.

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


Request.Headers["Referer"]
Request.ServerVariables

Gets a collection of HTTP headers.

Note that the Referer header is spelled differently than the HTTP_REFERRER server variable.

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 best 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.

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

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

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


Request.UrlReferrer

@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?

It should be noted that this property will throw a System.UriFormatException if the referer HTTP header is malformed.

Thanks! This worked fine, and you were the first! :)

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

You could use the UrlReferrer property of the current request:

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


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

My bad, it's a Uri not a string, Request.UrlReferrer.ToString() is fine, providing UrlReferrer != null.

Really... what happens if it's null? :-{

To safeguard against null, you can say: string actual = "" + Request.UrlReferrer ?? "(default)"; // (default) can be empty string

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


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

To safeguard against null, you can say: string actual = "" + Request.UrlReferrer ?? "(default)"; // (default) can be empty string

Note
Rectangle 27 0

c Getting the HTTP Referrer in ASP.NET?


Request.UrlReferrer
ServerVariables["HTTP_REFERER"]

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")

Note