2010-07-20 42 views
1

我有一個CustomRequestContext對象必須在每個請求後處理。我在Page_Load中創建並在Page_Unload中處理它。唯一的問題是,在某些情況下,我需要撥打Server.Transfer來重定向到另一個aspx頁面。在這種情況下,在新頁面準備好卸載之前,不應該卸載該對象。達到這個目標的最好方法是什麼?處理(可能重定向)請求後的對象

回答

0

我解決了這個問題,允許一次擁有一個頁面的對象。如果我想放棄頁面的控制,我會將它設置爲null,然後它不會在頁面的析構函數中釋放。

0

爲您的所有asp.net頁面創建一個自定義的PageBase類,如下所述,並在Page_LoadPage_Unload事件中處理CustomRequestContext。

/// <summary> 
/// Base of front end web pages. 
/// </summary> 
public class PageBase : System.Web.UI.Page 
{ 

    /// <summary> 
    /// Initializes a new instance of the Page class. 
    /// </summary> 
    public Page() 
    { 
     this.Load += new EventHandler(this.Page_Load); 
     this.UnLoad += new EventHandler(this.Page_UnLoad); 
    } 

    /// <summary> 
    /// Page Load 
    /// </summary> 
    /// <param name="sender">sender as object</param> 
    /// <param name="e">Event arguments</param> 
    private void Page_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      //Dispose the object here, assuming it is IDisposable. 
      //You can apply your own Disposition steps here.. 
      CustomRequestContext.Dispose(); 
     } 
     catch 
     { 
      //handle the situation gracefully here. 
     } 
    } 

    /// <summary> 
    /// Page UnLoad 
    /// </summary> 
    /// <param name="sender">sender as object</param> 
    /// <param name="e">Event arguments</param> 
    private void Page_UnLoad(object sender, EventArgs e) 
    { 
     try 
     { 
      //Dispose the object here, assuming it is IDisposable. 
      //You can apply your own Disposition steps here.. 
      CustomRequestContext.Dispose(); 
     } 
     catch 
     { 
      //handle the situation gracefully here. 
     } 
    } 
} 
+0

您正在處理它的加載和卸載功能。你的建議是我應該重新創建上下文對象而不是試圖保存它嗎? – Casebash 2010-07-20 10:28:13

+0

沒有。我剛纔介紹瞭如何在給定事件中處理對象的想法。什麼時候處理對象取決於你。 – 2010-07-20 10:56:59