ASPX:重定向,删除查询字符串,但不在浏览器中创建历史记录

发布于 2024-09-14 21:41:21 字数 603 浏览 2 评论 0原文

我正在寻找一种方法来摆脱页面的查询字符串并重定向到其自身,但以某种方式保留查询字符串数据。 示例:http://www.test.de/somepage.aspx?id=abc 应重定向到 http://www.test.de/somepage.aspx。尽管如此,在重定向之后,我希望能够获取最初传递的参数。而且我不想在浏览器历史记录中包含 http://www.test.de/somepage.aspx?id=abc

到目前为止我尝试过的:

  • Response.Redirect():在不创建浏览器历史记录的情况下进行正确的重定向,但我无法保留参数。

  • Server.Transfer:保留参数,但浏览器的URL保持不变。

  • 动态创建客户端表单并在 onload 中提交:有效,查询字符串消失,可以通过 Request.Form 访问参数,但会在浏览器中创建历史记录条目。

我目前唯一能想到的就是将参数存储在会话中,然后重定向,然后从那里获取它们。但也许还有另一种解决方案?

I'm looking for a way to get rid of the querystring of a page and redirect to itself but preserver the querystring data in some way.
Example: http://www.test.de/somepage.aspx?id=abc should redirect to http://www.test.de/somepage.aspx. Still, after the redirect, I want to be able to pick up the parameters that were originally passed. And I don't want to have http://www.test.de/somepage.aspx?id=abc in the browser's history.

What I tried so far:

  • Response.Redirect(): does a proper redirect without creating browser history but I cannot preserver the parameters.

  • Server.Transfer: preserves the parameters but the browser's URL remains unchanged.

  • Create a client form on the fly and submit in onload: works, querystring is gone, parameters are accessible through Request.Form, but creates a history entry in the browser.

The only thing I can currently think of is to store the parameters in the session, then redirect, then pick them up from there. But maybe there's still another solution?

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

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

发布评论

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

评论(2

醉生梦死 2024-09-21 21:41:21

正如 Pete 提到的,您可以将查询字符串参数保存在 Session 中,然后调用 Response.Redirect 以重定向到第二页。

Session["id"] = Request["id"];
Session["param2"] = Request["param2"];
Session["param3"] = Request["param3"];
....
Response.Redirect(sameurl);

在页面的第二次加载中检查查询字符串值是否消失。如果是,则不从查询字符串中读取值,而是从会话中读取值。

id = Session["id"];
param2 = Session["param2"];
param3 = Session["param3"];
...

As Pete mentioned, you can save your querystring parameters in Session and then call to Response.Redirect to redirect to the second page

Session["id"] = Request["id"];
Session["param2"] = Request["param2"];
Session["param3"] = Request["param3"];
....
Response.Redirect(sameurl);

In the second load of the page check if the querystring values are gone. If they are, instead of reading values from querystring read the values from session.

id = Session["id"];
param2 = Session["param2"];
param3 = Session["param3"];
...
慈悲佛祖 2024-09-21 21:41:21

在不了解为什么要这样做的情况下,最好的选择是将参数存储在会话状态中。但应尽量少用会话状态,因为它会导致应用程序不可靠。例如,如果您的用户存储书签或在会话超时后点击“刷新”,会发生什么情况?出于这些原因,我建议不要采用这种方法。

您可以考虑采用 URL 重写来使您需要的参数看起来更友好,例如: http:// /www.test.de/somepage/abc

Without getting into why you want to do this, your best bet is storing the parameters in the session state. Session state should be used minimally though as it leads to unreliable applications. E.g. what happens if your user stores a bookmark, or hits Refresh after the session has timed out? For these reasons, I'd advise against this approach.

You could consider employing URL rewriting to make the parameters you need look more friendly, for example like this: http://www.test.de/somepage/abc

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