Getting HTTP links in ASP.NET

I am looking for a quick, easy and reliable way to get the HTTP Referrer browser in ASP.Net ( C # ). I know that the HTTP Referrer itself is unreliable, but I want a reliable way to get the referrer, if present.

+80
Nov 23 '10 at 16:19
source share
8 answers

You can use the UrlReferrer property of the current request:

Request.UrlReferrer 

This will read the HTTP header.

+142
Nov 23 '10 at 16:22
source

Use the Request.UrlReferrer property.

Under the frame, the ServerVariables("HTTP_REFERER") property ServerVariables("HTTP_REFERER") simply checked.

+17
Nov 23 '10 at 16:22
source
 Request.Headers["Referer"] 

Explanation

Request.UrlReferer will throw a System.UriFormatException if the header of the HTTP header is rejected (which may happen because it is usually not under your control).

Regarding the use of Request.ServerVariables , for MSDN:

Request.ServerVariables Collection

The ServerVariables collection retrieves the values โ€‹โ€‹of the predefined environment variables and requests 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 environment variables, as well as headers, where Request.Headers is a much shorter list containing only headers.

Thus, the best solution is to use the Request.Headers collection to read the value directly. Pay attention to Microsoft warnings about encoding HTML values โ€‹โ€‹if you intend to display it in a form.

+12
Sep 05 '14 at 20:42 on
source

Like this: HttpRequest.UrlReferrer Property

 Uri myReferrer = Request.UrlReferrer; string actual = myReferrer.ToString(); 
+10
Nov 23 '10 at 16:24
source

Since Google sends you to this post when searching for the C# Web API Referrer there is a deal: the Web API uses a different Request type from a regular MVC Request called HttpRequestMessage , which does not include UrlReferrer . Since a regular Web API request does not include this information, if you really need it, you must have your clients to enable it. Although you can make this part of your API Object , it is better to use Headers .

First, you can extend HttpRequestMessage to provide the UrlReferrer() method:

 public static string UrlReferrer(this HttpRequestMessage request) { return request.Headers.Referrer == null ? "unknown" : request.Headers.Referrer.AbsoluteUri; } 

Then your customers need to install the Referrer Header in their API Request :

 // Microsoft.AspNet.WebApi.Client client.DefaultRequestHeaders.Referrer = new Uri(url); 

And now the Web API Request includes referrer data that can be accessed from this Web API :

 Request.UrlReferrer(); 
+7
Sep 08 '15 at 5:47
source
 string referrer = HttpContext.Current.Request.UrlReferrer.ToString(); 
+1
Nov 04 '16 at
source

I am using .Net Core 2 mvc, this works for me (to get a preview page):

 HttpContext.Request.Headers["Referer"]; 
0
Nov 22 '17 at 14:23
source

Someday you should provide an entire link like this

 System.Web.HttpContext.Current.Request.UrlReferrer.ToString(); 

(in the option when "Current" is not installed)

0
Dec 06 '17 at 15:01
source



All Articles