问题
Which one is better, Server.Transfer
or Response.Redirect
? I am looking for some explanation for this.
回答1:
They have different functions. Definition of better depends on what you are trying to do.
Response.Redirect tells the client to visit a new address, which can be anywhere.
Server.Transfer forwards the request (optionally preserving the query string) to another page on the same server.
If your criterion is cutting unnecessary overhead given that the new page is on the same server, Server.Transfer is the method you want.
回答2:
It depends on your reqiremnts.
Suppose if you are on page1.aspx and wants to go to page2.aspx
Response.Redirect scenario
page1.aspx calls Response.Redirect("page2.aspx",false); which sends a 302 redirect header down to the client browser, telling it that the requested (page1.aspx) has moved to page2.aspx, and the web application terminates. The client browser then sends a request to the webserver for page2.aspx. IIS tells asp_wp.exe to process the request. asp_wp.exe (after checking authentication and doing all the other setup stuff it needs to do when a new request comes in) instantiates the appropriate class for page2.aspx, processes the request, sends the result to the browser, and shuts down. In this case there is a roundtrip to the server.
Server.Transfer scenario
page1.aspx calls Server.Transfer("page2.aspx");. ASP.NET instantiates the appropriate class for page2.aspx, processes the request, sends the result to the browser, and shuts down.
Note that Server.Transfer cuts the load on the client and the server.
Server.Transfer is easier to code for, too, since you maintain your state. Information can be passed through the HTTP Context object between the pages, eliminating the need to pass information in the querystring or reload it from the database.
Some limitations of Server.Transfer
It can only work for same domain pages (on same server)
It bypasses any authentication on the page you transfer to
Now you can take decision yourself which one is better according to your requirements.
来源:https://stackoverflow.com/questions/5151439/which-one-is-better-server-transfer-and-response-redirect