Server.transfer changing the URL a second time
Asked Answered
C

1

6

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.

  1. I can't use response.redirect because I can't access the previous page from here.
  2. I tried using PostBackUrl, but that will not work for me because I need to save the data on the current page and then show the next page if no errors occurred. If the data was incorrect, and/or there were errors, then I need to show the user the same page.
  3. Now I thought to try server.transfer, but that is showing the URL of the previous page.

Can anybody point me in the right direction?

Cortisol answered 12/3, 2010 at 0:15 Comment(1)
I ended up using sessions and that solves the problem for me. But I would still like to know what is the solution for this problem.Cortisol
O
2

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.

Olympia answered 9/8, 2012 at 3:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.