How to get the previous url in C#?
Solution 1
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.
Solution 2
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.

Bengi Besçeli
Hi, I'm Bengi. I'm 42 years old and I'm a Web Developer since 1998.
Updated on April 16, 2020Comments
-
Bengi Besçeli over 2 years
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.
-
Adriano Repetti over 8 yearsYour previous page has to pass that information to new one (or you can store such information in session).
-
Bengi Besçeli over 8 yearsHow can I get this ? But I'm searching without using session.
-
-
Bengi Besçeli over 8 yearsBut it gets error: Object reference not set to an instance of an object.
-
Tasos K. over 8 yearsThe error occurs when the first page loads? If yes, add a check
if(Request.UrlReferrer != null) {...}
-
Bengi Besçeli over 8 yearsThis doen't work, it gets null, unfortinately (in code behind) (but using some query string parameters can fix my problem, thanks.
-
Bengi Besçeli over 8 yearsNo I checked in second page. (Im trying at an Asp.Net website)
-
Tasos K. over 8 yearsDoes the second page loads after a redirection? Then yes, it also should be null. I updated my answer by adding a few more information regarding that matter
-
Bengi Besçeli over 8 yearsI have check the new one (HTTP_REFERER) but its still gets null... I'm testing with blank new pages, redirecting from Default1.aspx to Default2.aspx, as you say and testing at Default2's PageLoad event.
-
Tasos K. over 8 yearsIf 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 pagedefault2.aspx
-
Bengi Besçeli over 8 yearsYep I see my fault and works now by clicking link. But is it possible redirecting in code behind ? What can I do for that ?
-
Tasos K. over 8 yearsWhen redirecting from code behind, you could follow @MatíasFidemraizer answer, there you could easily put a query string when doing the redirection
-
Bengi Besçeli over 8 yearsHello, in fact, I'm testing it by doing a response redirect in code behind.
-
Matías Fidemraizer over 8 years@Bengi And this is correct. You might be doing something wrong.
-
Bengi Besçeli over 8 yearsHmm, now I understand. Thank you again.
-
Bengi Besçeli over 8 yearsI 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.
-
Bengi Besçeli over 8 yearsI'm testing with blank new pages, redirecting from Default1.aspx to Default2.aspx, and testing at Default2's PageLoad event.
-
Gregbert about 2 yearsYour second example is the one I tried first. Worked like a charm!