CAN" EndResponse"提高ASP.Net页面的性能 [英] Can "EndResponse" increase performance of ASP.Net page

查看:237
本文介绍了CAN" EndResponse"提高ASP.Net页面的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在员工页有一个的Response.Redirect 。它重定向到工资页。

 的Response.Redirect(Salary.aspx);

这是工作的罚款,直到我说的异常如下处理。

 尝试
{
   的Response.Redirect(Salary.aspx);
}
赶上(异常前)
{
// MyLog();
    抛出新的异常();
}//剩余code在事件处理程序

这引起了新的异常说线程已被中止。我才知道,这可以通过设置 endResponse 为假为重定向是可以避免的。

 的Response.Redirect(网址,FALSE);
Context.ApplicationInstance.CompleteRequest();

新异常的解释:它总是抛出异常,而是由框架处理。由于我加一条try..catch它被抓获那里(和我举办了一个新的异常)

请注意: CompleteRequest 做搭桥进一步HTTP过滤器和模块,但它并不能绕过在当前页面生命周期进一步事件

请注意:Response.Redirect的抛出此异常来结束当前页面的处理。 ASP.NET的自己处理这个异常,并呼吁 ResetAbort 继续处理。


  1. 无论设置endResponse为假可以的增加由于异常的表现是不是抛出?

  2. 无论设置endResponse为假,可以降低因为页面生命周期事件的表现没有终止?

缺陷


  1. 如果您设置endResponse为,在事件处理剩余code将被执行。因此,我们需要做一个如果检查剩余的code(检查:如果重定向标准未达到)。

参考


  1. Response.Redirect导致System.Threading.ThreadAbortException

  2. ASP.NET例外"线程已被中止"导致方法来退出


解决方案

结束的响应(的Response.Redirect(URL)的Response.Redirect (URL,真))的不会的Response.Redirect更好的性能(URL,FALSE)。随着,因为你必须在code执行控制,你可以简单的情况下不执行任何更多的code你什么时候将用户重定向

这是在 MSDN条目中指定的Response.Redirect()


  

如果您指定endResponse参数true,则此方法调用原始请求时,它完成时抛出一个ThreadAbortException异常End方法。 此异常对Web应用程序的性能,这就是为什么通过虚假的endResponse参数推荐产生不利影响。


您确实需要关注的页面生命周期事件,正如你指出。你不应该继续执行该页面的事件,如果你打算将用户重定向(不仅性能)。我最近写了展示一个简单的例子会发生什么差编码/规划,如果你不这样做。

这帖子的底线是,的Response.Redirect()返回一个302浏览器。还有当你使用问题的潜在的Response.Redirect(网址,FALSE)因为页继续执行,并且用户可以选择忽略的302,而是看到页面那的会一直的被渲染...所以你需要采取步骤,以确保他们没有看到你不希望他们看到的任何东西。该 NoRedirect 附加为Firefox测试这个时候是有帮助的。

为了获得最佳性能:使用 endResponse 参数,确保您没有运行任何进一步的code,并确保该页面不会使你不希望用户查看,如果他们忽视了302的任何信息。

I have a Response.Redirect in my Employee page. It redirects to Salary page.

Response.Redirect ("Salary.aspx");

It was working fine until I added exception handling as below.

try
{
   Response.Redirect ("Salary.aspx");
}
catch(Exception ex)
{
//MyLog();
    throw new Exception();
}

//Remaining code in event handler

This caused a new exception saying "Thread was being aborted". I came to know that this can be avoided by setting endResponse as false for the redirect.

Response.Redirect(url, false);
Context.ApplicationInstance.CompleteRequest();

Explanation of new exception: It always throws the exception but handled by the framework. Since I added a try..catch it was caught there (and I am throwing a new exception)

Note: CompleteRequest does bypass further HTTP filters and modules, but it doesn't bypass further events in the current page lifecycle

Note: Response.Redirect throw this exception to end processing of the current page. ASP .Net itself handles this exception and calls ResetAbort to continue processing.

QUESTION

  1. Whether "setting endResponse as false" can increase performance since the exception is not thrown ?
  2. Whether "setting endResponse as false" can decrease performance since the page lifecycle events are not terminated?

PITFALL

  1. If you set endResponse as false, remaining code in the eventhandler will be executed. So we need to make a if check for the remaining code (Check: if redirection criteria was not met).

Reference

  1. Response.Redirect causes System.Threading.ThreadAbortException
  2. ASP.NET exception "Thread was being aborted" causes method to exit

解决方案

Ending the response (Response.Redirect(url) or Response.Redirect(url, true)) will not have better performance than Response.Redirect(url, false). With false, since you have control over the code execution, you can simply not execute any more code in the case when you are going to redirect the user.

This is specified in the MSDN entry for Response.Redirect():

If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes. This exception has a detrimental effect on Web application performance, which is why passing false for the endResponse parameter is recommended.

You DO need to be concerned about the page lifecycle events, as you noted. You shouldn't continue executing the page events if you are going to Redirect the user (not only for performance). I recently wrote a brief example showing what can happen with poor coding/planning if you don't.

The bottom line of that post is that Response.Redirect() returns a 302 to the browser. There is a potential for problems when you use Response.Redirect(url, false) since the page execution continues, and the user can choose to ignore the 302 and instead see the page that would've been rendered... so you need to take steps to ensure they don't see anything you don't want them to see. The NoRedirect add-on for Firefox is helpful when testing this.

For best performance: use "false" as the endResponse parameter, ensure you aren't running any further code, and ensure the page isn't going to render any information you don't want a user to see if they ignore the 302.

这篇关于CAN" EndResponse"提高ASP.Net页面的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆