Get previous page URL after Response.Redirect

28,121

Solution 1

You can pass the URL of the previous page to the error page's URL. something like

Response.Redirect("servererror/default.aspx?404&url=/folder/page.aspx")

And then get the url value on the error page and redirect it to the previous page.

Solution 2

Keep in mind what Response.Redirect is doing: It is issuing a 302 "found" to the client. This response code is meant to indicate that the content you are seeking is not in the location you requested, but instead is elsewhere.

Given that info; the page that issues the Response.Redirect is not actually the 'referrer' at all, then. This is why the page that referred to that page is still reported as the referred.

As rauts noted above, you should include in the URL string you are redirecting to any extra information you need, such as the current page's URL

Solution 3

You could try server.transfer instead to see if it populates the HTTP_REFERER value.

Solution 4

I know its a old post to reply Even the guy who have posted this question has found some workaround for solving his question............. but late yesterday night i was facing the same difficulty so i have come with a workaround which is working on my case...

I have 2 Pages Page1.aspx and Page2.aspx I have a Button1 and Button2 on the Page1.aspx now what the scenario is when i redirect to Page2.aspx i want to access Page1.aspx controls which is called Previous Page..... So i use PostBackURL of a button but in that case i was not getting the Click Event on CodeBehind......... so how should i solve this issue............. What i did is that i set the Button2.PostBackURL = "Page2.aspx" and when the Button1 Click event triggers i am registering the startup script which calls Button2.click event so now i am getting the Previous page controls..........

Here is the code..... if somebody looking for same functionality............ they will be helped in some sense........

Aspx Code

<asp:Button ID="btnSave" runat="server" Style="font-weight: 700" Text="Save" 
    onclick="btnSave_Click" />

Code-Behind

    protected void btnSave_Click(object sender, EventArgs e){
    try
    {
        StringBuilder objStringBuilder = new StringBuilder();
        objStringBuilder.AppendLine("<script type='text/javascript' language='javascript'>");
        objStringBuilder.AppendLine("document.getElementById('" + btnHidden.ClientID + "').click();");
        objStringBuilder.AppendLine("</script>");

        ClientScript.RegisterStartupScript(Page.GetType(),"Key1", objStringBuilder.ToString());
    }
    catch (Exception ex)
    {
        throw ex;
    }}

regards,

Share:
28,121
Jamie Taylor
Author by

Jamie Taylor

Updated on May 22, 2020

Comments

  • Jamie Taylor
    Jamie Taylor about 4 years

    I'm trying to get the previous page URL after I do a response write and i've looked around the web and people are using HTTP_REFERER but that doesn't work with Response.Redirect so is there anyway to get the URL of the previous page?

    I have this code on a few pages and i need to know which page it is coming from when it gets to the servererror/default.aspx page

     Response.Redirect("servererror/default.aspx?404")
    

    And on my servererror/default.aspx page i'm just trying to grab the previous page URL and put it into the Session Session("ErrorPage")

    Thanks

    Jamie

    UPDATE

    I have now got it to work like this

    Response.Redirect("server-error.aspx?404&" & Request.Url.ToString())
    

    That passes the URL of the page with the error to the next page and I then grab that from the Query String

    Thanks

    Jamie

  • jimplode
    jimplode over 13 years
    This does not populate the referrer field, but I believe it would populate the ClientFilePath as this is what they originally requested, and you then transfered them.
  • jimplode
    jimplode over 13 years
    This is the best solution, this is the approach most sites out there take.
  • eglasius
    eglasius over 13 years
    specially as HTTP_REFERER could be blocked all around in the browser (privacy)
  • Jamie Taylor
    Jamie Taylor over 13 years
    I adapted your suggestion to make this Response.Redirect("server-error.aspx?404&" & Request.Url.ToString()) and it works thanks