locked
MVC 3 Partial View Caching Not Honoring web.config settings RRS feed

  • Question

  • User327185541 posted

    I posted a question on stack overflow about how partial view output caching issues:

     @Html.Action("All", "Template")
     [OutputCache(CacheProfile = "Templates")]
       
    public ActionResult All()
       
    {
           
    return Content("This stinks.");
       
    }
    <caching>
     
    <outputCacheSettings>
       
    <outputCacheProfiles>
         
    <clear/>
         
    <add name="Templates" duration="3600" varyByParam="none"/>      
       
    </outputCacheProfiles>
     
    </outputCacheSettings>
     
    <outputCache  enableOutputCache="false" enableFragmentCache="false" />
    </caching>

    1. It errors out if you try to use a cache profile
    2. If you turn off caching in the web.config. IT STILL CACHES
    3. These settings work fine for normal view, just not partial view... 

    Tuesday, January 25, 2011 3:30 PM

Answers

All replies

  • User197322208 posted

    1. Output cache profile applies to PAGES. MVC caches THE ACTIONS, not the pages/views.

    2. What ?Please detail

    3. Even if it works now( BTW : what MVC do you use?)  the reccomended way is to use OutputcacheAttribute , not web.config.( see the similarity with Security?)

    I deeply sorry! unrellevant post!

    Tuesday, January 25, 2011 4:21 PM
  • User327185541 posted

    Ok, that doesn't seem all right.

     I'm using the same OutputcacheAttribute to decorate an action for rendering a view as with a partial view, I'm only using the web.config to change wheter it's on and for how long to cache. The difference is that the view action honors the setting but the partial view doesn't.

     If what you were saying were true then decorated actions that return views shouldn't work either.

    Tuesday, January 25, 2011 5:46 PM
  • User327185541 posted

    In short, I think there are several bugs in the current release of MVC 3.

    1. Validating child configuration code is wrong.
                private void ValidateChildActionConfiguration() {
                if (Duration <= 0) {
                    throw new InvalidOperationException(MvcResources.OutputCacheAttribute_InvalidDuration);
                }

                if (String.IsNullOrWhiteSpace(VaryByParam)) {
                    throw new InvalidOperationException(MvcResources.OutputCacheAttribute_InvalidVaryByParam);
                }

                if (!String.IsNullOrWhiteSpace(CacheProfile) ||
                    !String.IsNullOrWhiteSpace(SqlDependency) ||
                    !String.IsNullOrWhiteSpace(VaryByContentEncoding) ||
                    !String.IsNullOrWhiteSpace(VaryByHeader) ||
                    _locationWasSet || _noStoreWasSet) {
                    throw new InvalidOperationException(MvcResources.OutputCacheAttribute_ChildAction_UnsupportedSetting);
                }
            }
    2. Not honoring whether cache is enabled or not, is a big no-no.
    3. Not making it apparent through the api or error messages that something isn't supported
    
    

    If you mistakenly try to use any parameter but duration and varybyparam you will get an error saying that duration is not set, instead of an error like, "Hey we forgot to implement [enter setting name] in this situation, don't use it please". Basically that third statement will rarely if ever get hit.

    This feature probably should have been written as a separate attribute, or they could just implement the remaining attributes, or maybe just throw the correct error message.  The one big beef I have is that if I turn of caching in the web.config, it should turn off for all cases, not just pages, this is huge and very misleading to customers and I'm sure will stump a lot of people unknowingly.

    Tuesday, January 25, 2011 6:55 PM
  • User327185541 posted

    Wrote a quick blog post on how to modify the source to fix some of these issues:

    http://thenullreference.com/blog/fixing-the-asp-net-mvc-3-outputcacheattribute-for-partial-views-to-honor-some-web-config-settings/

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 8:26 PM
  • User401360897 posted

    One thing you must know about that

    Partial caching in MVC 3 doesn't use the standard ASP.NET APIs since they're insufficient for MVC's implementation

    http://forums.asp.net/p/1640980/4246810.aspx#4246810?Re%3a+Extensible+Partial+Output+Caching+in+ASP.NET+MVC+3

    Tuesday, January 25, 2011 11:35 PM