Response.Redirect() asp.net 的替代方案是什么?
您好,各种博客中“网站性能提示”中的提示之一是“避免重定向”。就我而言,我在同一页面使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
重定向是 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()
andServer.Transfer()
and which one must be used?". People used to sayTransfer
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.
建议针对不必要的重定向。
您的情况有所不同 - 您将信息传递到页面,这与常规重定向(即移动的页面)严格来说并不相同。
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).
您还可以执行
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.
主要的“邪恶”(如果可以这样称呼的话)是重定向需要额外的往返;客户端请求一个页面(通常是同一页面,指定单击了某个特定按钮),服务器响应说“改为请求此页面”,然后浏览器遵循,导致服务器实际上提供下一页。
有时有必要这样做,但是现在有更好的方法来控制网站中的导航。例如,您可以使用行为类似于超链接的 LinkButton,而不是导致回发和重定向的“表单”按钮,从而允许浏览器直接请求新页面。您还可以使用显示不同 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).
我认为如果您想重定向到同一页面,那么您可以在
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 useNextID
inViewState
also orHidden Field
so that you would not required to redirectSAME page
and then check thathidden field
orviewstate
instead of checkingQueryString
:D