Which one is better Server.Transfer and Response.Redirect

試著忘記壹切 提交于 2019-12-12 10:02:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!