2010-11-15 61 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