Server.transfer 第二次更改 URL

发布于 2024-08-24 15:52:53 字数 429 浏览 10 评论 0 原文

我正在使用 Asp.net 2.0。我从第 1 页到第 2 页执行 server.transfer。URL 保留在第 1 页。现在我单击第 2 页上的链接,这会将我转移到第 3 页。因此 URL 应保留在第 1 页相反,浏览器现在显示页面 2 的 URL。这是预期的行为吗?

我实际上是想隐藏 URL 中的参数。

  1. 我无法使用 response.redirect 因为我无法从此处访问上一页。
  2. 我尝试使用 PostBackUrl,但这对我不起作用,因为我需要保存当前页面上的数据,然后在没有发生错误的情况下显示下一页。如果数据不正确和/或存在错误,那么我需要向用户显示同一页面。
  3. 现在我想尝试 server.transfer,但那显示的是上一页的 URL。

有人能指出我正确的方向吗?

I am using Asp.net 2.0. I do a server.transfer from page 1 to page 2. The URL remains page 1. Now I click a link on page 2 and that will transfer me to page 3. So the URL should remain page 1. Instead the browser now shows the URL of page 2. Is that the expected behavior?

I was actually trying to hide the parameters from the URL.

  1. I can't use response.redirect because I can't access the previous page from here.
  2. I tried using PostBackUrl, but that will not work for me because I need to save the data on the current page and then show the next page if no errors occurred. If the data was incorrect, and/or there were errors, then I need to show the user the same page.
  3. Now I thought to try server.transfer, but that is showing the URL of the previous page.

Can anybody point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2024-08-31 15:52:53

这是预期的行为。

当您使用 Server.Transfer 时,ASP.NET 通过 ThreadAbortException 停止处理原始请求,然后立即在同一线程中开始处理新请求。新请求运行并将其输出发送到浏览器。浏览器对Server.Transfer一无所知。浏览器所知道的就是它请求了 page1 并且服务器发回了一些内容,作为开发人员,您知道这些内容实际上来自 page2。

如果要隐藏 page2 url,则回发是泄漏的地方。为了让 page2 内容处理回发,它必须回发到 page2。如果它回发到 page1,page1 将不知道如何处理视图状态和表单事件,因为这些事件实际上是由 page2 生成的。为了实现此目的,page2 提供的

元素的 actionpage2。在 Server.Transfer 之后查看浏览器中的 html 源代码,您会看到以下内容:

<form name="aspnetForm" method="post" action="Page2.aspx" id="aspnetForm">

使用传统的 Web 表单,对用户完全隐藏 URL 的唯一真正方法是不使用回发和让 page2 上的所有链接实际上链接回 page1 并将所有逻辑添加到 page1 以适当处理它。

或者,您根本无法使用回发。如果您通过 ajax 执行所有操作,则根本不会更改浏览器 url,并且无论如何您都应该能够为用户获得更好的体验。

更好的是使用 ASP.NET MVC,它将您推向友好的类似 REST 的 URL,这些 URL 非常容易让用户理解,并且您可以在内部映射到更复杂的参数。

This is expected behavior.

When you use Server.Transfer ASP.NET stops processing the original request via a ThreadAbortException and then immediately, in the same thread, begins processing the new request. The new request runs and sends its output to the browser. The browser doesn't know anything about the Server.Transfer. All the browser knows is that it requested page1 and the server sent it back some content, which as the developer you know is actually from page2.

Postbacks is where the page2 url leaks out, if it was intended to be hidden. In order for the page2 content to process a postback, it must postback to page2. If it posted back to page1, page1 wouldn't know what to do with the viewstate and form events since those are actually generated by page2. To accomplish this, the <form> element served by page2 has an action of page2. Look at your html source in the browser after the Server.Transfer, you'll see this:

<form name="aspnetForm" method="post" action="Page2.aspx" id="aspnetForm">

Using traditional webforms, the only real way to completely hide the URL from the user would be to not use postbacks and have all links on page2 actually link back to page1 and add all the logic to page1 to handle it appropriately.

Alternatively, you could not use postbacks at all. If you did all actions through ajax, then there would be no browser url change at all, and you should be able to get a better experience for the user anyways.

Even better would be to use ASP.NET MVC which pushes you towards friendly REST-like urls that are very easy for the user to understand and that you can map to more complex parameters internally.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文