高效的使用 Response.Redirect

介绍:php

  我正在评估一个 ASP.NET Web 项目应用。它有一些可扩展性问题。意味着当网站访问量增长的时候。系统将会变得缓慢。当我查看应用日志。我找到了大量的 ThreadAbortException. 这个应用大量的使用了 Response.Redirect (是的 endResponse= true),这个就是可扩展性问题的根源。经过endResponse = false 在Response.Redirect将会解决这个问题. 但这样作会致使应用程序出现一些奇怪的问题。由于应用程序将假设在 Response.Redirect 将在当前页面中止执行.除此以外你须要处理一些安全隐患,由于你的应用程序是假设页面事件永远不会执行重定向以后。在这篇文章中,我将讲述一个简单的方法来解决这些问题,并取得良好性能html

  说明:web

  比方说你有一个web表单,须要验证一些条件并在条件不符时重定向用户跳转。安全

1
2
3
4
5
6
7
8
9
10
11
12
protected void Page_Load( object sender, EventArgs e)
         {
             var condition = ......;
             if (!condition)
             {
                 Response.Redirect( "SomePage.aspx" );
             }
         }
         protected void btnSave_Click( object sender, EventArgs e)
         {
             // Save Data Here
         }

  这样作很好,但这会影响可扩展性能。由于它将会终止线程池.如今,只须要用Response.Redirect("Unauthorized.aspx", false)替换Response.Redirect("Unauthorized.aspx") . 这将解决线程终止的问题,但不会中止当前页面生命周期. 也就是说,你有须要确保 btnSave_Click 事件(和全部其余页面时间)由于只要容许btnSave_Click事件执行任何人均可以很容易地发送POST请求. 为了解决这个问题我推荐使用RedirectUser扩展方法。app

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public static class HttpResponseExtensions
     {
         public static void RedirectUser( this HttpResponse response, string url)
         {
             if (response.IsRequestBeingRedirected)
                 return ;
             response.Redirect(url, false );
             var context = HttpContext.Current;
             if (context != null )
             {
                 context.ApplicationInstance.CompleteRequest();
             }
         }
     }
     public partial class WebForm : System.Web.UI.Page
     {
         protected void Page_Load( object sender, EventArgs e)
         {
             var condition = .....;
             if (!condition)
             {
                 Response.RedirectUser( "Unauthorized.<span id=" 6_nwp " style=" width: auto; height: auto; float : none; "><a id=" 6_nwl " href=" http: //cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=281390770ff04e69&k=asp&k0=asp&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=694ef00f77901328&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3189%2Ehtml&urlid=0" target="_blank" mpid="6" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">asp</span></a></span>x");
             }
         }
         protected void btnSave_Click( object sender, EventArgs e)
         {
             if (Response.IsRequestBeingRedirected)
             {
                 return ;
             }
             // Save Data Here
         }
     }

  使用 RedirectUser 第一个好处是它将首先使用对于应用程序具备良好扩展性的Response.Redirect(with endResponse= false) 方法。.第二个好处就是在你屡次调用这个方法后它不会覆盖先前的Response.Redirect(若是有的话). 第三个好处是它会调用 HttpApplication.CompleteRequest用来处理  ASP.NET运行时全部经过的事件以及过滤 HTTP 管道信息(不是页面生命周期管道信息).另外你须要注意在 btnSave_Click事件中检查 Response.IsRequestBeingRedirected.我也但愿你把全部的内部控制放到 Response.IsRequestBeingRedirected 检查,asp.net

1
2
3
4
5
6
7
8
< form id = "form1" runat = "<span id=" 2_nwp" style = "width: auto; height: auto; float: none;" >< a id = "2_nwl" href = "http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=281390770ff04e69&k=server&k0=server&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=694ef00f77901328&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3189%2Ehtml&urlid=0" target = "_blank" mpid = "2" style = "text-decoration: none;" >< span style = "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >server</ span ></ a ></ span >">
         <% if(!Response.IsRequestBeingRedirected){ %>
             << span id = "3_nwp" style = "width: auto; height: auto; float: none;" >< a id = "3_nwl" href = "http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=18&is_app=0&jk=281390770ff04e69&k=asp&k0=asp&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=694ef00f77901328&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F3189%2Ehtml&urlid=0" target = "_blank" mpid = "3" style = "text-decoration: none;" >< span style = "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >asp</ span ></ a ></ span >:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
             <%--All the Other Controls--%>
             <%--All the Other Controls--%>
             <%--All the Other Controls--%>
         <%} %>
     </ form >

  另外一件你须要注意的事情,当你使用一个复杂的控制(相似GridView, RadGrid, etc)这些拥有 选择,插入,更新和删除事件时。 当 Response.IsRequestBeingRedirected 为true时,你必须取消操做(插入,更新或删除) 这些事件,下面是一个例子性能

1
2
3
4
5
6
7
8
protected void GridView1_RowEditing( object sender, GridViewEditEventArgs e)
         {
             if (Response.IsRequestBeingRedirected)
             {
                 e.Cancel = true ;
                 return ;
             }
         }

总结:优化

  在这篇文章里,我向您展现如何使用Response.Redirect . 我一样也发现了一些风险问题。能够采用Response.Redirect优化和技术以下降风险 .也一样但愿你喜欢这篇文章。 网站

  原文地址:using-response-redirect-effectively.aspxui

相关文章
相关标签/搜索