简体   繁体   English

为什么连续调用Server.Transfer会更改URL?

[英]Why does consecutive calls to Server.Transfer change the url?

I'm using Server.Transfer to load the contents of another page when clicking a button. 我正在使用Server.Transfer在单击按钮时加载另一个页面的内容。 This works as expected and the url stays the same. 这按预期工作,网址保持不变。 However, if I try to load a third page at the click of a button in the second page, the url changes to that of the second page. 但是,如果我尝试在第二页中单击按钮时加载第三页,则该URL将更改为第二页的URL。 I would expect it to keep the url of the first page. 我希望它能保留第一页的网址。

Example: 例:

  1. Page1.aspx loads. Page1.aspx加载。
    • Url: .../Page1.aspx 网址: .../Page1.aspx
    • Content: Page1.aspx 内容: Page1.aspx
  2. Server.Transfer("~/Page2.aspx") on button click. 单击按钮时的Server.Transfer("~/Page2.aspx")
    • Url: .../Page1.aspx 网址: .../Page1.aspx
    • Content: Page2.aspx 内容: Page2.aspx
  3. Server.Transfer("~/Page3.aspx") on button click 单击按钮时的Server.Transfer("~/Page3.aspx")
    • Url: .../Page2.aspx 网址: .../Page2.aspx
    • Content: Page3.aspx 内容: Page3.aspx

Why does the url change in the third step? 为什么网址会在第三步发生变化?

When you call Server.Transfer() the execution of the 1st page is stopped and the 2nd page is executed instead within the same HTTP request. 当您调用Server.Transfer() ,将停止执行第1页,并在同一HTTP请求中执行第2页。 The 2nd page (Page2.aspx) is then executed in the same way that it would be for a any normal request made directly to its URL, so the action of the form is set to its own URL (despite being called from Server.Transfer() ). 然后第二页(Page2.aspx)的执行方式与直接发送到其URL的任何正常请求的方式相同,因此表单的操作设置为自己的URL(尽管从Server.Transfer()调用) Server.Transfer() )。

If this was not the case then when the form is submitted it would post to Page1.aspx (as you are expecting), but this page would not necessarily contain the same controls and be able to process the request. 如果不是这种情况,那么当提交表单时,它将发布到Page1.aspx(正如您所期望的那样),但此页面不一定包含相同的控件并且能够处理请求。

这可能会有所帮助:

Response.Redirect("~/page.aspx",false)

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

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