Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server.transfer changing the URL a second time

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?

like image 543
Yogesh Jindal Avatar asked Mar 12 '10 00:03

Yogesh Jindal


People also ask

What is difference between Server transfer and response redirect?

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.

What is the usage of Server transfer?

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.

Which of the following is the correct difference between Server transfer and response?

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.

Which method is used to redirect user from one page to another?

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.


1 Answers

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.

like image 67
Samuel Neff Avatar answered Oct 05 '22 22:10

Samuel Neff