Search code examples
c#asp.netserver.transfer

Browser URL changing even after using Server.Transfer


I am using Asp.net 4.0. I do a server.transfer from page 1 to page 2. The URL remains page 1. Now I click a link on page 2 and that will transfer me to page 3. So the URL should remain page 1. Instead the browser now shows the URL of page 2. Is that the expected behaviour?

I was actually trying to hide the Page URL.

Can anybody help me on this.

I know there is duplicate question on stack overflow but it doesn't have any convincing answer and question is 4 years old too. Please Help me with this or suggest better way to achieve this

Code :

On page1

Btn1_Click(object sender, EventArgs e)
{

server.Transfer("Page2.aspx");

}

On Page2

Btn2_Click(object sender, EventArgs e)
{

server.Transfer("Page3.aspx");

}

Solution

  • Remember "Server.Transfer does not change the URL in the address bar"

    Check this site you will get clear idea about Server.Transfer and Response.Redirect

    https://www.youtube.com/watch?v=xJVjRUHXYbE&index=54&list=PL6n9fhu94yhXQS_p1i-HLIftB9Y7Vnxlo

    When Using Server.Transfer

    For ex:

    Page1.aspx, Page2.aspx

    In page1.aspx assume code in button click like this

    Server.Transfer("~/page2.aspx");

    Then you will redirect to page2.aspx but url shown in address bar is same i.e; page1.aspx eventhough you are in page2.aspx.

    when you use Response.Redirect it will show page2.aspx when you redirected to page2.aspx

    Coming To your Question:

    This is an expected behavior.

    You asking Url changes if you use Server.Transfer in second time

    If you run page1.aspx for first time then it shows

    Address Bar : `http://localhost:1234/WebSite3/Page1.aspx`
    
    Action Name : "page1.aspx"//see pagesource
    

    if you click button in page1.aspx it will shows page2.aspx but with same url like:

    Address Bar : `http://localhost:1234/WebSite3/Page1.aspx`  but different
    
    Action Name="page2.aspx"//see pagesource
    

    if you click button in page2.aspx it will shows page3.aspx but with different url like:

    Address Bar : `http://localhost:1234/WebSite3/Page2.aspx`  but different 
    
    Action Name="page3.aspx"//see pagesource
    

    Here you find different url why because eventhough your url is page1.aspx but your request comes from page2.aspx see page source.So for this reason the url changes from page1.aspx to page2.aspx.