Alex
Alex

Reputation: 197

HttpContext.Current.Request.UrlReferrer is always null ASP.NET MVC

UrlReferrer is always null in Firfox

What I want to do is to get the URL of coming request

So I thought to pass the URL from the Razor view to controller in response header that way

Razor View

@{ 
    string url = HttpContext.Current.Request.UrlReferrer.ToString();
    HttpContext.Current.Response.Headers.Add("CustomeUrl", url);
}

and get the hearder value like that way in the controller

Controller

public string SaveUploadedFile(HttpPostedFileBase fileBase)
{
    string url = HttpContext.Current.Request.Headers.Keys["CustomeUrl"].ToString();
}

but unfortunately the above code didn't work because HttpContext.Current.Request.Headers.Keys["CustomeUrl"].ToString(); is null

Please help me to pass the url from View to Controller

Upvotes: 4

Views: 15541

Answers (2)

NightOwl888
NightOwl888

Reputation: 56859

The reason why HttpContext.Current.Request.Headers.Keys["CustomeUrl"].ToString() is always null in your controller is because you are adding the header to HttpContext.Current.Response in your view and reading it from HttpContext.Current.Request in your controller. Since the view and controller both execute on the server and share the same Request/Response model, you need to read it from the same location that you write it.

That said, since both the View and Controller have access to the same Request/Response data. There is no need to pass a value that already exists in HTTP context to the controller because it already has it. Also, the UrlReferrer property will throw an exception if the URL is malformed (which can happen since it is not usually under your direct control). Therefore, you should use the Request.Headers collection to retrieve it. Therefore, this line will work the same in your controller and view.

string referrer = HttpContext.Request.Headers["Referer"];

Side Note: You shouldn't use the legacy static HttpContext.Current accessor in MVC because there is no way to abstract it (and therefore it may be different from what is available in HttpContextBase if you are using custom or 3rd party components that change it).

HTTP referer is only non-null if a hyperlink is clicked on in the browser that takes you to the URL of the MVC application.

Page A

hosted at: http://example.com/page-a.html

<html>
<head></head>
<body>
    <a href="http://example2.com/page-b">click me</a>
</body>

Page B (MVC)

hosted at: http://example2.com/page-b

var referrer = HttpContext.Request.Headers["Referer"];
// referrer value is http://example.com/page-a.html if the above 
// hyperlink is clicked on page-a.html. If the user types the
// new URL in the browser instead of clicking the hyperlink, 
// HTTP referer will usually be null, depending on the browser implementation.

NOTE: HTTP referer is completely dependent upon the client and any firewalls that exist between the client and server. Some firewalls can strip headers out so applications should not rely on them in order to function (or at the very least have a fallback plan in case the header is not available). This information should generally only be used for tracking/debugging purposes.

Upvotes: 4

Carlos Ferreira
Carlos Ferreira

Reputation: 432

You don't need to go though all that trouble, just use Request.UrlReferrer on the controller.

If it's null, there can be other causes.

Also, the UrlReferrer is sent by the browser so is pointless to add it to the response.

Upvotes: 0

Related Questions