I am using Asp.net 2.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 behavior?
I was actually trying to hide the parameters from the URL.
response.redirect
because I can't access the previous page from here.Can anybody point me in the right direction?
So, in brief: Response. Redirect simply tells the browser to visit another page. Server. Transfer helps reduce server requests, keeps the URL the same and, with a little bug-bashing, allows you to transfer the query string and form variables.
Server. Transfer navigates the pages within the same application or within the same server, the page is still in memory that can read the values directly from page2 on page1, in other words by using server. Transfer the page is not redirected permanently.
The Response. Redirect method redirects a request to a new URL and specifies the new URL while the Server. Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page.
Approach: To redirect from an HTML page to another page, you can use the <meta> tag by specifying the particular link in the URL attribute. It is the client-side redirection, the browsers request the server to provide another page.
This is expected behavior.
When you use Server.Transfer
ASP.NET stops processing the original request via a ThreadAbortException
and then immediately, in the same thread, begins processing the new request. The new request runs and sends its output to the browser. The browser doesn't know anything about the Server.Transfer
. All the browser knows is that it requested page1 and the server sent it back some content, which as the developer you know is actually from page2.
Postbacks is where the page2 url leaks out, if it was intended to be hidden. In order for the page2 content to process a postback, it must postback to page2. If it posted back to page1, page1 wouldn't know what to do with the viewstate and form events since those are actually generated by page2. To accomplish this, the <form>
element served by page2 has an action
of page2
. Look at your html source in the browser after the Server.Transfer
, you'll see this:
<form name="aspnetForm" method="post" action="Page2.aspx" id="aspnetForm">
Using traditional webforms, the only real way to completely hide the URL from the user would be to not use postbacks and have all links on page2 actually link back to page1 and add all the logic to page1 to handle it appropriately.
Alternatively, you could not use postbacks at all. If you did all actions through ajax, then there would be no browser url change at all, and you should be able to get a better experience for the user anyways.
Even better would be to use ASP.NET MVC which pushes you towards friendly REST-like urls that are very easy for the user to understand and that you can map to more complex parameters internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With