3

How to get the previous url in a MasterPage in C# ?

I'm trying to find the page which is redirected from.

Thanks in advance.

2
  • 1
    Your previous page has to pass that information to new one (or you can store such information in session). Commented Aug 3, 2014 at 20:50
  • How can I get this ? But I'm searching without using session. Commented Aug 3, 2014 at 20:51

4 Answers 4

17

You can get information of the previous url with the UrlReferrer property. This works in MVC and Web forms.

Request.UrlReferrer.AbsoluteUri

Note that in the first page the property Request.UrlReferrer will be null. Also, it will be null if a redirection occurs (e.g. when a user logs into the web page).

This property is based on the HTTP_REFERER variable, so you could use this one instead.

Request.ServerVariables["HTTP_REFERER"]

Since the HTTP_REFERER is a variable sent by the client it might be altered or removed by the request. Also, the variable is not set when the referre url starts with https.

This article mentions a few points why the Request.UrlReferrer can be null.

9
  • But it gets error: Object reference not set to an instance of an object. Commented Aug 3, 2014 at 21:49
  • The error occurs when the first page loads? If yes, add a check if(Request.UrlReferrer != null) {...}
    – Tasos K.
    Commented Aug 3, 2014 at 21:51
  • No I checked in second page. (Im trying at an Asp.Net website) Commented Aug 3, 2014 at 21:53
  • 1
    If you are doing a redirect, it should be null. It will not be null if the user is in page default1.aspx and clicks a links to the page default2.aspx
    – Tasos K.
    Commented Aug 3, 2014 at 22:31
  • 1
    Your second example is the one I tried first. Worked like a charm!
    – Gregbert
    Commented Oct 9, 2020 at 18:34
3

Usually you use a query string parameter to achieve this: current?previousUrl=/some/11.

This will allow you to access this value from the server-side code using Context.Request.QueryString["previousUrl"] in your master page code-behind.

5
  • This doen't work, it gets null, unfortinately (in code behind) (but using some query string parameters can fix my problem, thanks. Commented Aug 3, 2014 at 21:51
  • Hello, in fact, I'm testing it by doing a response redirect in code behind. Commented Aug 4, 2014 at 9:28
  • @Bengi And this is correct. You might be doing something wrong. Commented Aug 4, 2014 at 11:51
  • I didn't try with using url parameters, but I'm sure it will work with url parameters... The thing is; I need to solve this within code-behind (not by using url parameters). But I think this is not possible upon @Tasos K.'s post. Commented Aug 5, 2014 at 10:56
  • I'm testing with blank new pages, redirecting from Default1.aspx to Default2.aspx, and testing at Default2's PageLoad event. Commented Aug 5, 2014 at 11:02
1

As of .NET 6.0 the UrlReferrer object is deprecated from the Request (HttpRequest) object. It is now stored as a part of the Request.Headers dictionary.

The new way to access the UrlReffer object would be:

string urlReferrer = Request.Headers["Referer"]!.ToString();

Note: this object may be null in certain cases. It would be most safe to first check if the object is null before accessing it.

string? urlReferrer = null;
if (Request.Headers.ContainsKey("Referer"))
{
    urlReferrer = Request.Headers["Referer"]!.ToString();
}
1
  • You can use Request.Headers.Referer now
    – Thomas V
    Commented May 30 at 11:12
0
string urlName = Request.UrlReferrer.ToString();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.