[Mono-list] Page Caching - VaryByParam, VaryByCustom (Bug or Feature)

Marc DM m at phronein.com
Mon Sep 12 01:37:22 EDT 2005


After a little bit of research this is what I found out using XSP 1.0.9 :

Two files global.asax and index.aspx. (see below). A simple web 
application to demonstrate what I think is a flaw in the handling of 
the  @OutputCache directive of *.aspx pages.

According to the M$DN documentation :
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcachingversionsofpagebasedoncustomstrings.asp
 when using VaryByCustom if you do not intend to use VaryByParam you 
should set the attribute value to "None".

That's ok. But doesn't this mean that I should be able to use it if I 
please?

The example below (taken from the MSDN page) should give me a different 
version for each value of pg on the querystring, as well as change 
depending on browserversion.

However, it would seem as though, the inclusion of the VaryByCustom 
attribute supercedes VaryByParam.

In my tests, I get the same cached version generated by a change in the 
GetVaryByCustomString regardless of what I specify for ?pg=.

A workaround I'm using (see below) is to just include all my cache 
varying parameters as a comma-delimited VaryByCustom string and parse it 
in global.asax to return a string unique to the  requested version of 
the page.

See code below.

My question is, is this behaviour expected (doubt it). Should I fix it, 
or is it something easy for y'all to fix?

Regards,

Marc DM

------------------------------
[global.asax]

    <%@ Application Language="c#"%>
    <script runat="server">
    public override string GetVaryByCustomString(HttpContext context,
    string arg){
      if (arg == "minorversion")
        return "Version=" + context.Request.Browser.Version.ToString();
      else
        return "NoVersion";
    }
    </script>

[/global.asax]

[index.aspx]

    <%@ Page Language="c#" %>
    <%@ OutputCache Duration="300" VaryByParam="pg"
    VaryByCustom="minorversion" %>
    <Script language="C#" runat="server">
        public void Page_Load(Object sender, EventArgs e) {
            browserversion.InnerHtml = Request.Browser.Version.ToString();
            timestamp.InnerHtml = DateTime.Now.ToString("r");
        }
    </Script>

    Varying output by custom string (BrowserVersion): <B
    id="browserversion" runat="server"/>
    <BR>
    Added to the cache: <B id="timestamp" runat="server" />

    <br>
    Click Links below and observe the timestamp on various pages with
    different browsers.
    <br>
    <a href="index.aspx?pg=1">index.aspx?pg=1</a><br>
    <a href="index.aspx?pg=2">index.aspx?pg=2</a><br>
    <a href="index.aspx?pg=3">index.aspx?pg=3</a><br>
    <a href="index.aspx?pg=4">index.aspx?pg=4</a><br>

[/index.aspx]

---------------------------------------------------

[global.asax - workaround]

    <%@ Application Language="c#"%>
    public override string GetVaryByCustomString(HttpContext context,
    string arg){
      string[] args = arg.Split(',');
      string ret="";
     
      for(int i=0;i<args.Length;i++)
      { 
          if (args[i].Trim() == "browserversion")
                ret += "BrVer=" +
    context.Request.Browser.Version.ToString() + ";" ;
          if (args[i].Trim() == "pg")
                ret += "pg=" + Request.QueryString["pg"] + ";";    
          /*
          if (args[i].Trim() == "my-other-arg")
          {
              // perform action to generate string to identify page version
              ret += "Another ID String"  + ";";
          }
          */
      }
      return ret;
    }
    </script>

[/global.asax - workaround]

[index.aspx - workaround]

    <%@ Page Language="c#" %>
    <%@ OutputCache Duration="300" VaryByParam="none"
    VaryByCustom="pg,browserversion" %>
    <Script language="C#" runat="server">
        public void Page_Load(Object sender, EventArgs e) {
            browserversion.InnerHtml = Request.Browser.Version.ToString();
            qs_pg.InnerHtml = Request.QueryString["pg"];
            timestamp.InnerHtml = DateTime.Now.ToString("r");
        }
    </Script>

    Varying output by custom string (BrowserVersion): <B
    id="browserversion" runat="server"/>
    <br>
    Varying output by custom string (Querystring[pg]): <B id="qs_pg"
    runat="server"/>
    <br>
    Added to the cache: <B id="timestamp" runat="server" />
    <br>
    <a href="index.aspx?pg=1">index.aspx?pg=1</a><br>
    <a href="index.aspx?pg=2">index.aspx?pg=2</a><br>
    <a href="index.aspx?pg=3">index.aspx?pg=3</a><br>
    <a href="index.aspx?pg=4">index.aspx?pg=4</a><br>

[/index.aspx - workaround]



More information about the Mono-list mailing list