如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面

为每个session页配制缓存是通过 @ OutputCache 指令的属性 VaryByCustom 来实现的。按照下面的脚本(包含到一个.aspx文件中),花上10秒钟就可以为各个session ID用户配制独立的页面版本。

<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="SessionID" %>

要用到 VaryByCustom="SessionID" ,就要在在服务器的根目录下有Global.asax文件,在文件中声明以下用法:

<script language="C#" runat="server">
  public override string GetVaryByCustomString(HttpContext context, string arg)
  {
      if (arg.ToLower () == "sessionid") {
          HttpCookie cookie =
           context.Request.Cookies["ASP.NET_SessionId"];
           if (cookie != null)
              return cookie.Value;
      }
      return base.GetVaryByCustomString (context, arg);
  }
</script>

GetVaryByCustomString 是ASP.NET页面输出缓存的基础。它继承于HttpApplication,返回从session cookie得到的session ID。用户在调用GetVaryByCustomString时会被关联到一个session。

这个技术的缺点是不能用于不支持cookie的用户。另外,用户只有在第二次发出申请时才能获得缓存,因为第一次请求没有合法的session cookie。

http://www.cnblogs.com/xiaotaoliang/archive/2005/10/30/264727.html

 

请问@OutputCache是用于单独一个用户的,还是所有用户的

可以缓存到不同的地方,而引响的用户也不同,如果缓存在客户端,就针到个人用户,   如果缓存在服务器,那就是针对所有用户,也可以缓存在代理服务器上面。

原文地址:https://www.cnblogs.com/emanlee/p/1659470.html