Response.Redirect和server.Transfer有什么区别? [英] what is the difference between Response.Redirect and server.Transfer?

查看:81
本文介绍了Response.Redirect和server.Transfer有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




Response.Redirect和server.Transfer有什么区别?



如果有人知道请让我知道。



谢谢

M.Dhanasekaran

Hi
what is the difference between Response.Redirect and server.Transfer?

If any one knows please let me know.

Thanks
M.Dhanasekaran

推荐答案

搜索在谷歌上你可以得到你所有小问题的答案:





Server.Transfer():客户端显示在它上面仅请求页面,但所有内容都是请求的页面。可以使用Context.Item集合将数据保留在页面中,这是将数据从一个页面传输到另一个页面的最佳方式之一,使页面状态保持活动状态。







Response.Dedirect():客户端知道物理位置(页面名称和查询字符串为好)。 Context.Items在转移到目标页面时失去了持久性。在早期版本的IIS中,如果我们想要将用户发送到新的Web页面,我们唯一的选择就是Response.Redirect。虽然这种方法确实实现了我们的目标,但它有几个重要的缺点。最大的问题是此方法会将每个页面视为单独的事务。除了难以维护您的事务完整性之外,Response.Redirect还引入了一些额外的麻烦。首先,它阻止了良好的代码封装。其次,您无法访问Request对象中的所有属性。当然,有一些解决方法,但它们很难。最后,Response.Redirect需要往返客户端,在高容量站点上会导致可伸缩性问题。

正如您可能怀疑的那样,Server.Transfer修复了所有这些问题。它通过在服务器上执行传输而不需要往返客户端来执行此操作。
Searched on google you can get answers to all your small questions :


Server.Transfer() : client is shown as it is on the requesting page only, but the all the content is of the requested page. Data can be persist accros the pages using Context.Item collection, which is one of the best way to transfer data from one page to another keeping the page state alive.



Response.Dedirect() :client know the physical loation (page name and query string as well). Context.Items loses the persisitance when nevigate to destination page. In earlier versions of IIS, if we wanted to send a user to a new Web page, the only option we had was Response.Redirect. While this method does accomplish our goal, it has several important drawbacks. The biggest problem is that this method causes each page to be treated as a separate transaction. Besides making it difficult to maintain your transactional integrity, Response.Redirect introduces some additional headaches. First, it prevents good encapsulation of code. Second, you lose access to all of the properties in the Request object. Sure, there are workarounds, but they’re difficult. Finally, Response.Redirect necessitates a round trip to the client, which, on high-volume sites, causes scalability problems.
As you might suspect, Server.Transfer fixes all of these problems. It does this by performing the transfer on the server without requiring a roundtrip to the client.


Server.Transfer v / s Response.Redirect



服务器响应都是ASP.NET的对象。 Server.Transfer和Response.Redirect都用于将用户从一个页面转移到另一个页面。两者都用于相同的目的,但两者之间仍存在一些差异如下:



如果你想转移用户,从语法上来说两者都是不同的对于名为newpage.aspx的页面,两种方法的语法都是,



Server.Transfer v/s Response.Redirect

Both "Server" and "Response" are objects of ASP.NET. Server.Transfer and Response.Redirect both are used to transfer a user from one page to another page. Both are used for the same purpose but still there are some differences are there between both that are as follows:

Syntactically both are different, if you want to transfer the user to a page named newpage.aspx then syntax for both methods will be,

Response.Redirect("newpage.aspx") and Server.Transfer("newpage.aspx")



Response.Redirect 涉及往返到服务器而 Server.Transfer 通过避免往返来节省服务器资源。它只是将Web服务器的焦点更改为不同的页面,并将页面处理转移到另一个页面。



往返意味着响应.Redirect 它首先将新页面的请求发送到浏览器然后浏览器将新页面的请求发送到Web服务器然后在您的页面更改后但是在Server.Transfer的情况下它直接与服务器更改页面因此它在整个过程中保存了往返。



如果您使用的是Server.Transfer,那么您可以直接访问值,控件和属性你不能用Response.Redirect做的上一页。



假设你目前在 Page1.aspx 上,现在你是使用Response.Redirect将用户转移到Page2.aspx然后当请求Page2页面时,Page1已从服务器的内存中刷新,除非开发人员显式,否则无法检索有关它的信息y使用会话,cookie,应用程序,缓存等技术保存信息。但是在Server.Transfer变量的情况下,变量可以保留在范围内,Page2可以直接从Page1读取属性,因为它仍然在内存中,因为你知道Server.Transfer只是将焦点从page1更改为page2所以在这种情况下,浏览器不知道发生了任何更改,这就是为什么使用此方法可以访问有关上一页的信息。



Response.Redirect 更改浏览器地址栏中的URL。所以他们可以加入书签。而Server.Transfer会在浏览器的地址栏中保留原始URL。它只是用新的内容替换上一页的内容。



实际上在 Server.Transfer 的情况下它直接与网络服务器联系以获取新的页面请求,它不涉及浏览器,因此浏览器不知道发生了任何变化。但是如果您知道Response.Redirect首先将请求(针对新页面)发送到浏览器,那么将执行进一步的处理,因此浏览器知道浏览器窗口中有一些更改,这就是为什么它更改URL在地址栏中。

所以,问题浏览器地址栏中的地址没有变化如果你从安全的角度来看它是一件好事,但如果你发现它会产生问题刷新页面或者如果要添加该页面的书签。如果是刷新和书签,它将添加执行地址栏中当前存在的URL的操作,但是如您所知,Server.Transfer不会更改URL,因此有时会产生问题。



Response.Redirect可用于.aspx和html页面,而Server.Transfer只能用于.aspx页面,并且特定于ASP和ASP.NET。



使用Response.Redirect,您可以将用户重定向到两种类型的页面.html或.aspx,如下所示,


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.

Roundtrip means in case of Response.Redirect it first sends the request for the new page to the browser then browser sends the request for the new page to the webserver then after your page changes But in case of Server.Transfer it directly communicate with the server to change the page hence it saves a roundtrip in the whole process.

If you are using Server.Transfer then you can directly access the values, controls and properties of the previous page which you can’t do with Response.Redirect.

Suppose you are currently on the Page1.aspx and now you are transferring the user to the Page2.aspx using Response.Redirect then When the Page2 page is requested, Page1 has been flushed from the server’s memory and no information can be retrieved about it unless the developer explicitly saved the information using some technique like session, cookie, application, cache etc. But in case of Server.Transfer variables can stay in scope and Page2 can read properties directly from Page1 because it’s still in memory, as you know the Server.Transfer just changes the focus from page1 to page2 So in this case browser doesn’t know that any change is happen there that’s why with this method you can access the information about the previous page.

Response.Redirect changes the URL in the browser’s address bar. So they can be bookmarked. Whereas Server.Transfer retains the original URL in the browser’s address bar. It just replaces the contents of the previous page with the new one.

Actually in case of Server.Transfer it directly contact with the webserver for the new page request, it doesn’t involve the browser, so that browser doesn’t know that there is any change happen. But in case of Response.Redirect as you know it first send the request (for new page) to the browser then further processing will be performed, so here browser knows that yes there is some change in the browser window that’s why it changes the URL in the address bar.
So, the matter "No change of address in the browser address bar" This is a good thing if you see it from security point of view but it creates problem in case if you refresh your page or in case you want to add bookmark of that page. In case of refresh and bookmark it will add perform both the action with the URL currently present in the address bar, but as you know Server.Transfer doesn’t changes the URL, so sometimes it creates problem.

Response.Redirect can be used for both .aspx and html pages whereas Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET.

With Response.Redirect you can redirect the user to the both type of pages .html or .aspx like below,

Response.Redirect("mypage.html") OR Response.Redirect("OtherPage.aspx")



但是在Server.Transfer的情况下,你只能使用.asp或.aspx如下页面

Server.Transfer(mypage.asp)或Server.Transfer(OtherPage.aspx)



响应。重定向可用于将用户重定向到外部网站。 Server.Transfer只能在同一服务器上运行的站点上使用。您不能使用Server.Transfer将用户重定向到在不同服务器上运行的页面。



假设在网页上的某些操作我想将用户重定向到 http://www.yahoo.com 所以使用 Response.Redirect 您可以将用户重定向到外部网站,但万一Server.Transfer只能使用您网站上的.asp或.aspx页面。



现在的问题是使用哪个页面什么时候使用?大多数情况下使用Server.Transfer方法是因为Server.Transfer更快,因为往返次数少一些,但有些人说不建议使用Server.Transfer,因为操作通常会流经几个不同的页面,因为你失去了正确的页面URL,但同样取决于你的要求。

Server.Transfer还允许更灵活,因为你可以使用HTTPContext.Items在页面之间传递变量,所以,当你使用Server.Transfer时需要传递上下文项。否则使用Response.Redirect,这样用户将始终在地址栏中看到正确的URL。



我提到了最常见的差异,如果有人知道除了然后请给出你的意见。



谢谢,



Jigar Malik


But in case of Server.Transfer you can only work with .asp or .aspx page like below
Server.Transfer("mypage.asp") OR Server.Transfer("OtherPage.aspx")

Response.Redirect can be used to redirect a user to an external websites. Server.Transfer can be used only on sites running on the same server. You cannot use Server.Transfer to redirect the user to a page running on a different server.

Suppose on some action on the webpage I want to redirect my user to the http://www.yahoo.com so with Response.Redirect you can redirect your user to the external site, but in case of Server.Transfer you can only work with the .asp or .aspx pages that are present in your site.

Now the question is which to use and when to use? Mostly the Server.Transfer method is preferable to use because Server.Transfer is faster since there is one less roundtrip, but in some people say that Server.Transfer is not recommended since the operations typically flow through several different pages due to which you lose the correct URL of the page, but again all depends on your requirement.
Server.Transfer also allows for more flexibility since you can use HTTPContext.Items to pass variables between pages so, use Server.Transfer when you need to pass context items. Otherwise use Response.Redirect so the user will always see the correct URL in the address bar.

I mentioned the most common differences here if anyone knows any other difference other than these then please give your comments.

Thank you,

Jigar Malik


Respose.redirect

---------------------------------- -----------------

Respose.redirect可以使用另一个appliaction网址。

它有往返。

它比server.transfer慢得多。



Server.transfer

---------- -----------------------------------------

In server.transfer我们只能在那个应用程序中使用。

它没有往返。

它比response.redirect快
Respose.redirect
---------------------------------------------------
Respose.redirect can use another appliaction url.
It has round trip.
Its much slower than server.transfer.

Server.transfer
---------------------------------------------------
In server.transfer we can use with in that application only.
It has not round trip.
it has faster than response.redirect


这篇关于Response.Redirect和server.Transfer有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆