Caching ChildActions using cache profiles won't work?

前端 未结 5 1629
粉色の甜心
粉色の甜心 2020-12-03 08:13

I\'m trying to use cache profiles for caching child actions in my mvc application, but I get an exception: Duration must be a positive number.

My web.config looks li

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-03 08:17

    I got around the problem by creating a custom OutputCache attribute, that manually loads the Duration, VarByCustom and VarByParam from the profile:

    public class ChildActionOutputCacheAttribute : OutputCacheAttribute
    {
        public ChildActionOutputCacheAttribute(string cacheProfile)
        {
            var settings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection("system.web/caching/outputCacheSettings");
            var profile = settings.OutputCacheProfiles[cacheProfile];
            Duration = profile.Duration;
            VaryByParam = profile.VaryByParam;
            VaryByCustom = profile.VaryByCustom;
        }
    }
    

    The advantage of this approach is that you get to still keep all your profiles in just one place in the web.config.

提交回复
热议问题