介紹:
我正在評(píng)估一個(gè) ASP.NET Web 項(xiàng)目應(yīng)用。它有一些可擴(kuò)展性問(wèn)題。意味著當(dāng)網(wǎng)站訪問(wèn)量增加的時(shí)候。系統(tǒng)將會(huì)變得緩慢。當(dāng)我查看應(yīng)用日志。我找到了大量的 ThreadAbortException. 這個(gè)應(yīng)用大量的使用了 Response.Redirect (是的 endResponse= true),這個(gè)就是可擴(kuò)展性問(wèn)題的根源。通過(guò)endResponse = false 在Response.Redirect將會(huì)解決這個(gè)問(wèn)題. 但這樣做會(huì)導(dǎo)致應(yīng)用程序出現(xiàn)一些奇怪的問(wèn)題。因?yàn)閼?yīng)用程序?qū)⒓僭O(shè)在 Response.Redirect 將在當(dāng)前頁(yè)面停止執(zhí)行.除此之外你需要處理一些安全隱患,因?yàn)槟愕膽?yīng)用程序是假設(shè)頁(yè)面事件永遠(yuǎn)不會(huì)執(zhí)行重定向之后。在這篇文章中,我將講述一個(gè)簡(jiǎn)單的方法來(lái)解決這些問(wèn)題,并取得良好性能
說(shuō)明:
比方說(shuō)你有一個(gè)web表單,需要驗(yàn)證一些條件并在條件不符時(shí)重定向用戶跳轉(zhuǎn)。
復(fù)制代碼 代碼如下:
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
}
這樣做很好,但這會(huì)影響可擴(kuò)展性能。因?yàn)樗鼘?huì)終止線程池.現(xiàn)在,只需要用Response.Redirect("Unauthorized.aspx", false)替換Response.Redirect("Unauthorized.aspx") . 這將解決線程終止的問(wèn)題,但不會(huì)停止當(dāng)前頁(yè)面生命周期. 也就是說(shuō),你有需要確保 btnSave_Click 事件(和所有其他頁(yè)面時(shí)間)因?yàn)橹灰试SbtnSave_Click事件執(zhí)行任何人都可以很容易地發(fā)送POST請(qǐng)求. 為了解決這個(gè)問(wèn)題我推薦使用RedirectUser擴(kuò)展方法。
復(fù)制代碼 代碼如下:
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.aspx");
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (Response.IsRequestBeingRedirected)
{
return;
}
// Save Data Here
}
}
使用 RedirectUser 第一個(gè)好處是它將首先使用對(duì)于應(yīng)用程序具有良好擴(kuò)展性的Response.Redirect(with endResponse= false) 方法。.第二個(gè)好處就是在你多次調(diào)用這個(gè)方法后它不會(huì)覆蓋先前的Response.Redirect(如果有的話). 第三個(gè)好處是它會(huì)調(diào)用 HttpApplication.CompleteRequest用來(lái)處理 ASP.NET運(yùn)行時(shí)所有通過(guò)的事件以及過(guò)濾 HTTP 管道信息(不是頁(yè)面生命周期管道信息).另外你需要注意在 btnSave_Click事件中檢查 Response.IsRequestBeingRedirected.我也希望你把所有的內(nèi)部控制放到 Response.IsRequestBeingRedirected 檢查,
復(fù)制代碼 代碼如下:
form id="form1" runat="server">
% if(!Response.IsRequestBeingRedirected){ %>
asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
%--All the Other Controls--%>
%--All the Other Controls--%>
%--All the Other Controls--%>
%} %>
/form>
另一件你需要注意的事情,當(dāng)你使用一個(gè)復(fù)雜的控制(類(lèi)似GridView, RadGrid, etc)這些擁有 選擇,插入,更新和刪除事件時(shí)。 當(dāng) Response.IsRequestBeingRedirected 為true時(shí),你必須取消操作(插入,更新或刪除) 這些事件,下面是一個(gè)例子
復(fù)制代碼 代碼如下:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
if (Response.IsRequestBeingRedirected)
{
e.Cancel = true;
return;
}
}
總結(jié):
在這篇文章里,我向您展示如何使用Response.Redirect . 我同樣也發(fā)現(xiàn)了一些風(fēng)險(xiǎn)問(wèn)題??梢圆捎肦esponse.Redirect優(yōu)化和技術(shù)以降低風(fēng)險(xiǎn) .也同樣希望你喜歡這篇文章。