2010-11-15 58 views
7

我正在使用輸出緩存作爲登錄系統的網站。我有全球網頁,每個用戶都可以訪問。這些頁面被緩存並且也使用母版頁。VaryByCustom不適用於會話變量

<%@ OutputCache Duration="3600" VaryByParam="none" VaryByCustom="userid" %> 

我在會話中存儲用戶登錄詳細信息。我的global.asax文件在這裏:

public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
    string result = String.Empty; 
    if (arg == "userid") 
    { 
     object o = Session["UserID"]; 
     if (o != null) { result = o.ToString(); } 
    } 
    else { result = base.GetVaryByCustomString(context, arg); } 
    return result; 
} 

我有一個母版頁中的面板,這對認證用戶是可見的。當用戶登錄並查看公共網頁A與其它來賓用戶也看到在頁面A.身份驗證的用戶面板如果客人先查看頁面A然後驗證的用戶不會看到在頁面A.面板

哪一部分我的代碼是錯誤的?我第一次使用VaryByCustom

編輯

我修改我的Global.asax這樣的,但沒有被寫在文本文件中:

public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
    string result = String.Empty; 

    FileInfo t = new FileInfo(Server.MapPath("App_Data\\debug.txt")); 
    StreamWriter Tex = t.AppendText(); 
    Tex.WriteLine("GetVaryByCustomString: " + arg); 

    if (arg == "userid") 
    { 
     object o = Session["UserID"]; 
     if (o != null) { result = o.ToString(); } 

     Tex.WriteLine("Checked UserID: " + o + Tex.NewLine);    
    } 
    else { result = base.GetVaryByCustomString(context, arg); } 

    Tex.Close(); 

    return result; 
} 
+0

好的,我發現我無法訪問當前會話變量「會話狀態在此上下文中不可用」。試圖現在解決它。 – 2010-11-16 09:56:16

回答

0

我認爲這可能是會議[「用戶名」]對於一些原因總是返回空/或者有時返回null,即使用戶被認證。

仔細檢查您在此功能請求之前設置了它。

+0

我看到會話變量已更改,但問題在於GetVaryByCustomString未被解僱/正常工作我認爲 – 2010-11-16 09:39:13

+0

@hasanGursoy在某處存在問題,但如果調用了該函數,則會話參數未設置。 – Aristos 2010-11-16 10:36:05

+1

@hasanGursoy檢查http://www.aspmessageboard.com/showthread.php?t=174916 – Aristos 2010-11-16 10:38:21