高效的使用 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

相關文章
相關標籤/搜索