0

I am trying to capture a referring URL using the following code. The redirect to the page is not working. Please help me to fix the problem.

  if (!Page.IsPostBack)
    {
        ViewState["PreviousPage"] = Request.ServerVariables["HTTP_REFERER"];
    }

  protected void btnLogin_Click(object sender, EventArgs e)
  {
    if (ViewState["PreviousPage"] != null)
       {
          Response.Redirect(ViewState["PreviousPage"].ToString());
       }
  }
4
  • 1
    It's not working. What is not working? Any errors? What are you expecting it to do? In what method is your first IF statement? Commented Jul 24, 2013 at 19:20
  • ^ Exactly! If you inspect the ViewState["PreviousPage"] value after setting it, what does it contain? Is it empty? Does the redirect happen, but to the wrong URL? Commented Jul 24, 2013 at 19:27
  • I assume your if statement is within you page_load eventHandler? What error msg do you get? Commented Jul 24, 2013 at 19:29
  • There can be the case that the Referrer isn't set because someone directly went to your login page. Is this case handled properly? Commented Jul 24, 2013 at 19:47

4 Answers 4

1

I'm not sure it will help, but you can also try to access it with the url referrer property of the request object.

Request.UrlReferrer
Sign up to request clarification or add additional context in comments.

2 Comments

I am pretty sure that the way the OP is trying won't work, whereas I've been using the property you suggest and it always works. +1.
I am not getting any error message. I am coming to this page from, e.g previous.aspx but after clicking the button, it is redirecting to that page.
1

It seems to me like you may be rebuilding the wheel here.

There's a built in .NET way to do this with forms authentication by default. You'll get a querystring to redirect you to a page in the application automatically. You hit a page while not logged in, the app remembers where you wanted to go before you logged in, and sends you there when you authenticate.

Is that what you're trying to do?

2 Comments

I am not using .net authentication.
OK - a referrer will only be populated if the user clicks on a link. Redirects, meta refreshes, server.transfer and typing in a URL directly all will set the referer to null. Keep that in mind as you're designing this.
0

There's a few things that could be going on here.

If you are directly visiting the login page, the referer is always going to be null. So make sure your code can handle that.

You haven't said where if (!Page.IsPostBack) is, if it's in the Page_Init your problem is most likely that ViewState["PreviousPage"] = Request.ServerVariables["HTTP_REFERER"]; is not going to persist the ViewState during postback.

You can fix that by either moving the check into Page_Load or adding ViewState.SetDirty(true); to Page_InitComplete.

Another possible issue is that some browser strip out the HTTP_REFERER header for privacy reasons. Again, make sure your code handles the value being null.

2 Comments

I am following the following link. codeproject.com/Articles/91641/…
So following that link, when you're on the Management page or Development page you click the "Go to Default Page" button which takes you to the Default page. There you click the "Back" button which takes you to the previous page. What happens when you click the "Back" button? ArgumentNullException? Nothing? 404?
0

This might work

Response.Redirect((ViewState["PreviousPage"]+".aspx"));

1 Comment

I know the OP has not given a lot of details, but can you explain why this might work?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.