Response.Redirect vs Server.Transfer - redirect as a "suggestion"
So I read an article on Response.Redirect that say's "its like a suggestion" as compared to Server.Transfer which happens whether the client wants to or not. Wh开发者_StackOverflow社区at does this mean? Is there some kind of event that we can offer a user to say "well nope nm I don't want to redirect to that page"?
Server.Transfer
will execute the new page immediately and send its result to the client.
The client stays at the original URL doesn't see anything unusual.
Response.Redirect
sends a special code, called an HTTP 302, that tells the client to send a new request to a different URL.
Response.Redirect
Response.Redirect
will navigate to the url specified.
From MSDN
Any response body content such as displayed HTML text or Response.Write text in the page indicated by the original URL is ignored. However, this method does send other HTTP headers set by this page indicated by the original URL to the client. An automatic response body containing the redirect URL as a link is generated. The Redirect method sends the following explicit header, where URL is the value passed to the method, as shown in the following code:
Server.Transfer
Server.Transfer
will execute the url passed in but maintain the url transfered from.
From MSDN
When you use the Transfer method, the state information for all the built-in objects are included in the transfer. This means that any variables or objects that have been assigned a value in session or application scope are maintained. In addition, all of the current contents for the Request collections are available to the .asp file that is receiving the transfer.
Here's an article discussing and comparing the two.
It's "like a suggestion" in the sense that when the client requests a url that's redirected, the server responds with "that object has moved...find it here." When you do Server.Transfer, you immediately respond with the output of the page to which you're transferring.
There's nothing in either process you could hook into to give the user any choice...you'd have to respond with a 200 and implement the choice to the user through logic.
Response.Redirect involves a "roundtrip" to the server whereas Server.Transfer conserves server resources by "avoiding the roundtrip". It just changes the focus of the webserver to a different page and transfers the page processing to a different page.
精彩评论