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.