简体   繁体   English

Response.Redirect()或HREF?

[英]Response.Redirect() or A HREF ?

I see a lot of articles talking about how to use it. 我看到很多关于如何使用它的文章。 But what is the advantage of using Response.redirect in your c#, versus just using ahref in the .aspx file? 但是,与仅在.aspx文件中使用ahref相比,在c#中使用Response.redirect有什么好处?

Response.Redirect runs on the ASP.NET Server. Response.Redirect在ASP.NET服务器上运行。 It can only be using when the server is processing an existing HTTP request. 它只能在服务器正在处理现有HTTP请求时使用。 It sends a redirect status code and location to redirect back to the client as part of an HTTP response. 它发送重定向状态代码和位置,以作为HTTP响应的一部分重定向回客户端。 Then client can then take that location and send an HTTP request to it. 然后,客户端可以转到该位置并向其发送HTTP请求。 So you end up with: 因此,您最终得到:

Client -> Server: Initial request for resource at location X
Server -> Client: Redirect to get resource from location Y
Client -> Server: Request for resource at location Y

A hyperlink (a href) is before any HTTP request is even generated. 甚至在生成任何HTTP请求之前,都没有超链接(a href)。 When you click on a hyperlink, the browser just sends an HTTP request to the location specified by the href. 当您单击超链接时,浏览器仅将HTTP请求发送到href指定的位置。 The browser could then response with a redirect. 然后,浏览器可以使用重定向进行响应。

They're really apples and oranges, because they operate at different times. 它们实际上是苹果和橙子,因为它们在不同的时间运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM