none
OutputCache's VaryByParam doesn't seem to be working RRS feed

  • Question

  • This is a sample demo that I have created to try explaining myself about VaryByParam attribute of OutputCache in ASP.NET:

    All the markup is written in the Default.aspx page.

    The Markup of Default.aspx:

    <%@ OutputCache Duration="10" VaryByParam="ID" %>


    <asp:Button ID="SetQueryStringButton1" runat="server" Text="Set query string ID=1 and redirect"
    PostBackUrl="~/OutputCachingUsingQueryString/Default.aspx?ID=1" />
    <br />
    <asp:Button ID="SetQueryStringButton2" runat="server" Text="Set query string ID=2 and redirect"
             
    PostBackUrl="~/OutputCachingUsingQueryString/Default.aspx?ID=2" />
    <br />
    <asp:Button ID="SetQueryStringButton3" runat="server" Text="Set different query string and redirect"
               
    PostBackUrl="~/OutputCachingUsingQueryString/Default.aspx?ABCD=3" />

    The code-behind of Default.aspx:

    if (IsPostBack)
    {
    string key = Request.QueryString.GetKey(0);
     
    MessageLabel.Text = "The value of query string " + key + " is : " + Request.QueryString[key] + "<br/>";
     
    MessageLabel.Text += DateTime.Now.ToLongTimeString();
    }

    When I click first two buttons, then according to the definition of VaryByParam, it should cache the page for ID = 1 and ID = 2, which it does. But when i click the third button, it caches the page for ABCD = 3 too. What i was expecting is the page shouldn't be cached for ABCD=3, because I haven't specified it in VaryByParam. I get the same DateTime when i repeatedly click the third button.

    Please provide insights of what wrong I am doing. I don't understand the reason that the page gets cached when I click the third button.

    Thanks in advance.

    Thursday, May 17, 2012 4:12 AM

Answers

All replies

  • Hi Anup,

    Thank you for posting on this forum.

    This issue is more related to ASP.net, so please try to ask it on the dedicated forum: http://forums.asp.net/

    In addition, it seems that you have misunderstanding the property VaryByParam: Here is an example can explain it: 

    1. Keep all your code the same.

    2. Add one more button like this:

        <asp:Button ID="SetQueryStringButton1" runat="server" Text="Set query string ID=1 and redirect"
                PostBackUrl="~/Default.aspx?ID=1" />
                <br /> 
        <asp:Button ID= "SetQueryStringButton2" runat="server" Text="Set query string ID=2 and redirect"
               PostBackUrl="~/Default.aspx?ID=2" />
               <br />
        <asp:Button ID="SetQueryStringButton3" runat="server" Text="Set different query string and redirect"
                PostBackUrl="~/Default.aspx?ABCD=3" />
                <br />
        <asp:Button ID="SetQueryStringButton4" runat="server" Text="Set different query string and redirect"
                PostBackUrl="~/Default.aspx?ABCD=4" />

    When you click on the new added button, you can found, the value of ABCD is not changed during the duration.

    I hope this will be helpful.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 18, 2012 8:51 AM
    Moderator
  • Hello Mike,

    Thanks for pointing me out to the right forum.

    I have posted my question under this URL:

    http://forums.asp.net/t/1805057.aspx/1?VaryByParam+of+OutputCache+doesnt+seem+to+be+working

    I haven't got any satisfactory answer.

    This issue still remains the mystery for me.

    Can you please comment further on the post.

    Thanks in advance.

    Monday, May 21, 2012 4:11 AM
  • Hi Anup,

    Actually, I totally agree with Ruchira. I think he explained the VaryByParam very well.

    Based on the discussion, it seems that you want to keep the value unchanged. So, as Ruchira suggested, you can set VaryByparam = "ID,ABCD", this means when the parameter is in the list (ID,ABCD), it will show the cached page.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 21, 2012 5:19 AM
    Moderator
  • Hello Mike,

    Thanks for your response.

    I need exactly opposite of that. I only want ASP.NET to cache pages for ID and not for ABCD.

    The problem is that even if i write VaryByParam="ID" (which is what I want) , still it is caching pages for ABCD also.

    EDIT:

    If possible for you, can you please run the sample I have written to achieve this scenario. I only want ASP.NET to cache pages for ID param, not for ABCD.  I have also posted this issue in StackOverFlow. Please let me know how to solve this issue, i am stuck in my project.

    Regards,

    Anup Vasudeva



    Monday, May 21, 2012 5:46 AM
  • Hi Anup,

    I did run your sample.

    Let's make this more clear:

    Click on this button: 

     <asp:Button ID="SetQueryStringButton1" runat="server" Text="Set query string ID=1 and redirect"
                PostBackUrl="~/Default.aspx?ID=1" />

    You want get "The value of query string ID is : 1", right?

    When click on this button: 

    <asp:Button ID= "SetQueryStringButton2" runat="server" Text="Set query string ID=2 and redirect"
               PostBackUrl="~/Default.aspx?ID=2" />

    You want to get "The value of query string ID is : 2", right?

    When click on this button: 

    <asp:Button ID="SetQueryStringButton3" runat="server" Text="Set different query string and redirect"
                PostBackUrl="~/Default.aspx?ABCD=3" />

    You want to get "The value of query string ABCD is : 3", right?

    When click on this button: 

    <asp:Button ID="SetQueryStringButton4" runat="server" Text="Set different query string and redirect"
                PostBackUrl="~/Default.aspx?ABCD=4" />

    You want to get "The value of query string ABCD is : 3", right?

    If not, please correct me.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 21, 2012 6:55 AM
    Moderator
  • Hello Mike,

    Yes, but I am also writing this statement too in the code-behind (the last line): MessageLabel.Text += DateTime.Now.ToLongTimeString();

    STEP 1). When the user clicks the first button:

    EXPECTED: The user should get a fresh date time from the server, as the user has requested the page with query string ID = 1 for the first time. If the user again clicks the first button, ASP.NET will serve the page from the cache, because VaryByParam="ID" is mentioned in the OutputCache directive.

    OUTPUT: The value of the query string ID is 1 (<br/>) 5/21/2012 12:41:23

    STEP 2). When the user clicks the second button:

    EXPECTED: The user should get a fresh date time from the server, as the user has requested the page with query string ID = 2  for the first time. If the user again clicks the second button, ASP.NET will serve the page from the cache, because VaryByParam="ID" is mentioned in the OutputCache directive.

    OUTPUT: The value of query string ID is 2 (<br/>) 5/21/2012 12:41:25

    STEP 3). When the user clicks the third button: (for the first time)

    EXPECTED: The user should get a fresh date time from the server, as the user has requested the page with query string ABCD = 3 for the first time. But ASP.NET will not cache this page because ABCD is not mentioned in the VaryByParam attribute.

    OUTPUT: The value of query string ABCD is 3 (<br/>) 5/21/2012 12:41:30

    STEP 4). When the user again clicks the third button:

    EXPECTED: As per the OuputCache directive, we have specified, VaryByParam="ID". It means ASP.NET should not have cached the page when the user clicked the third button for the first time. So the user should again get a fresh date time from the server because there is no cached version of the page with query string ABCD.

    OUTPUT: The value of query string ABCD is 3 (<br/>) 5/21/2012 12:41:35  <-- A new datetime
    WHAT I AM EXPERIENCING:  ASP.NET is serving the page from the cache, and hence the output is:  The value of query string ABCD is 3 (<br/>) 5/21/2012 12:41:30

    Regards,

    Anup Vasudeva


    Monday, May 21, 2012 7:31 AM
  • Hi Anup,

    Yes, now this is very clear. Thank you for your clarification.

    Although, I'd rather to refer the MSDN documentation again:

    1. http://msdn.microsoft.com/en-us/library/hdxfb6cy.aspx and this one: http://msdn.microsoft.com/en-us/library/xsbfdd8c.aspx 

    2. http://msdn.microsoft.com/en-us/library/system.web.ui.outputcacheparameters.varybyparam.aspx

    Gets a semicolon-delimited list of query string or form POST parameters that the output cache usesto vary the cache entry.

    This property is used to set how the server change the cache. The parameter "VaryByParams" means the server will change the cache by query parameters. So all these are based on we will cache the pages. So the server cached the pages ABCD=3, too. But you didn't say when the value of this parameter changes, the cache need to be updated(you only set VaryByParams = ID), This is why you didn't get the updated time.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, May 21, 2012 8:50 AM
    Moderator
  • Hello Mike,

    Thanks a lot for your clarification.

    What I thought was since ABCD is not specified in VaryByParam, so ASP.NET won't consider caching pages for ABCD=3.

    But now I realize that ASP.NET would still cache the page solely because of OutputCache directive only, and VaryByParam has nothing to do with it, because we haven't specified ABCD in VaryByParam.

    Sorry for putting you in so much trouble. :)

    You have a great day ahead.

    Monday, May 21, 2012 9:33 AM
  • Hi Anup,

    I am glad I am helpful and you are clear to this issue.

    Nice day to you, too.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 21, 2012 9:38 AM
    Moderator