Monday, June 9, 2008

Server.Transfer and the Changing URL

When you use Server.Transfer to move the user from, let's say, Page A to Page B, the URL bar still displays Page A because the client browser makes a request to the server for Page A and gets a response. This is unlike Response.Redirect where the client browser is suggested a different URL and it starts a new request. Think of it as an RSS news aggregator site that goes to other servers to fetch content and your client browser only sees the URL of the RSs news aggregator site (although it is a slightly different concept).

Now that we're on Page B (with the URL bar still showing Page A), you can click a button (or anything similar, for that matter) that has a click event handler to perform a Server.Transfer to a Page C. Now, you will notice that the address bar shows the URL for Page B. The reason for this change is that Page B (while accessed through the URL of Page A), caused a postback that was to Page B (with the URL of Page B). The client browser now requests Page B, and therefore displays the URL of Page B in the address bar, and gets Page C as the response from the server.

It's one of those things most developers would not expect unless they've tried it, discussed it or read about it, which is why you find this article up here to create an awareness of this behavior in ASP.NET.

No comments: