How to get the previous url in C#?

21,061

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.

Share:
21,061
Bengi Besçeli
Author by

Bengi Besçeli

Hi, I'm Bengi. I'm 42 years old and I'm a Web Developer since 1998.

Updated on April 16, 2020

Comments

  • Bengi Besçeli
    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
      Adriano Repetti over 8 years
      Your previous page has to pass that information to new one (or you can store such information in session).
    • Bengi Besçeli
      Bengi Besçeli over 8 years
      How can I get this ? But I'm searching without using session.
  • Bengi Besçeli
    Bengi Besçeli over 8 years
    But it gets error: Object reference not set to an instance of an object.
  • Tasos K.
    Tasos K. over 8 years
    The error occurs when the first page loads? If yes, add a check if(Request.UrlReferrer != null) {...}
  • Bengi Besçeli
    Bengi Besçeli over 8 years
    This doen't work, it gets null, unfortinately (in code behind) (but using some query string parameters can fix my problem, thanks.
  • Bengi Besçeli
    Bengi Besçeli over 8 years
    No I checked in second page. (Im trying at an Asp.Net website)
  • Tasos K.
    Tasos K. over 8 years
    Does 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
    Bengi Besçeli over 8 years
    I 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.
    Tasos K. over 8 years
    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
  • Bengi Besçeli
    Bengi Besçeli over 8 years
    Yep 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.
    Tasos K. over 8 years
    When 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
    Bengi Besçeli over 8 years
    Hello, in fact, I'm testing it by doing a response redirect in code behind.
  • Matías Fidemraizer
    Matías Fidemraizer over 8 years
    @Bengi And this is correct. You might be doing something wrong.
  • Bengi Besçeli
    Bengi Besçeli over 8 years
    Hmm, now I understand. Thank you again.
  • Bengi Besçeli
    Bengi Besçeli over 8 years
    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.
  • Bengi Besçeli
    Bengi Besçeli over 8 years
    I'm testing with blank new pages, redirecting from Default1.aspx to Default2.aspx, and testing at Default2's PageLoad event.
  • Gregbert about 2 years
    Your second example is the one I tried first. Worked like a charm!