Response.Redirect() asp.net 的替代方案是什么?

发布于 2024-10-15 19:25:51 字数 260 浏览 3 评论 0原文

您好,各种博客中“网站性能提示”中的提示之一是“避免重定向”。就我而言,我在同一页面使用 Response.Redirect。我正在传递一个查询字符串并向用户显示适当的信息。

Response.Redirect("FinalPage.aspx?NextID=" + ID);

因此,在我们的业务逻辑中,我正在使用不同的信息重新加载同一页面。

那么如何避免重定向呢?还有其他选择吗?顺便说一句,我的目标是在那里获得一些表现。

Hi One of the tips in "website performance tips" in various blogs says "Avoid Redirects". In my case, I am using Response.Redirect for the same page. I am passing a querystring and displaying appropriate information to the user.

Response.Redirect("FinalPage.aspx?NextID=" + ID);

So in our business logic, i am reloading the same page with different information.

So how do i avoid redirect? Is there any other alternative? BTW, my aim is to gain some performance there.

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

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

发布评论

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

评论(5

╰つ倒转 2024-10-22 19:25:51

重定向是 PRG 模式中的 R,它是处理已发布请求的可接受模式。所以它绝对不是邪恶的。

然而,曾经有一个常见的面试问题Server.Redirect()Server.Transfer() 之间有什么区别 code> 以及必须使用哪一个?”。人们过去常说“传输”,因为它不涉及往返,但从那时起网络已经发生了很大的变化。在那些日子里,除非使用 Transfer 或 Redirect,否则您无法重用视图中的通用逻辑,但现在,特别是使用 ASP NET MVC,有很多方法可以做到这一点。

就你而言,我完全支持PRG并且我相信重定向是语义上更正确。如果用户单击 F5 或刷新,它还可以防止重新提交表单。

Redirect is the R in the PRG pattern which is an accepted pattern for processing posted requests. So it is definitely not evil.

However, there used to be a common interview question: "What is the difference between Server.Redirect() and Server.Transfer() and which one must be used?". People used to say Transfer because it did not involve a round-trip but web has changed so much since then. In those days you could not re-use the the common logic in the views unless you use Transfer or Redirect, but nowadays especially with ASP NET MVC there are tons of a ways to do that.

In your case, I am all for PRG and I believe redirect is semantically more correct. Also it prevents the form being re-submited if user clicks F5 or refresh.

清风夜微凉 2024-10-22 19:25:51

建议针对不必要的重定向。

您的情况有所不同 - 您将信息传递到页面,这与常规重定向(即移动的页面)严格来说并不相同。

The recommendation is for unnecessary redirects.

Your case is different - you are passing in information to the page, this is not strictly the same thing as a regular redirect (i.e. a page that moved).

携余温的黄昏 2024-10-22 19:25:51

您还可以执行Server.Transfer,它不需要新的请求进来,从而减轻服务器上的负载。比较两者的更多信息位于此处

在您的情况下,您确实想要执行重定向,因为您正在修改查询字符串并更改页面上的某些内容,而不是将初始请求的处理转移到另一个页面。

You can also do a Server.Transfer, which does not require a new request to come in, thus lessening the load on the server. More information comparing the two is here.

In your case, you do want to do a Redirect because you are modifying the query string and changing something on the page, as opposed to shifting processing of the initial request to another page.

歌入人心 2024-10-22 19:25:51

主要的“邪恶”(如果可以这样称呼的话)是重定向需要额外的往返;客户端请求一个页面(通常是同一页面,指定单击了某个特定按钮),服务器响应说“改为请求此页面”,然后浏览器遵循,导致服务器实际上提供下一页。

有时有必要这样做,但是现在有更好的方法来控制网站中的导航。例如,您可以使用行为类似于超链接的 LinkBut​​ton,而不是导致回发和重定向的“表单”按钮,从而允许浏览器直接请求新页面。您还可以使用显示不同 ASCX 的 MultiView,并通过视图翻转来控制导航(但是,请理解这可能有其自身的性能影响,特别是在以嵌套方式使用它们时)。

The main "evil" if it could be called such is that redirects require an extra round trip; the client requests one page (usually the same page, specifying that a particular button was clicked), and the server responds saying "request this page instead", and the browser then complies, resulting in the server actually serving up the next page.

It's sometimes necessary to do this, however there are now much better ways to control navigation in a website. For instance, instead of a "form" button that causes a postback and redirect, you could use a LinkButton that will behave like a hyperlink, allowing the browser to request the new page directly. You could also use a MultiView that shows different ASCXs, and control navigation by view-flipping (however, understand that this can have its own performance implications, especially when using them in a nested fashion).

野心澎湃 2024-10-22 19:25:51

我认为如果您想重定向到同一页面,那么您可以在 Response.Redirect("FinalPage.aspx?NextID=" + ID); 中使用 NextID 而不是执行 Response.Redirect("FinalPage.aspx?NextID=" + ID); code>ViewState 也或 隐藏字段,这样您就不需要重定向相同页面,然后检查隐藏字段 或 < code>viewstate 而不是检查 QueryString

:D

I think if you want to redirect to same page then instead of doing Response.Redirect("FinalPage.aspx?NextID=" + ID); you could use NextID in ViewState also or Hidden Field so that you would not required to redirect SAME page and then check that hidden field or viewstate instead of checking QueryString

:D

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