【问题标题】:ChildActionOnly output cache not disabledChildActionOnly 输出缓存未禁用
【发布时间】:2020-01-18 08:40:47
【问题描述】:

我在标有[ChildActionOnly] 的控制器中有以下操作:

[OutputCache(Duration = 3600)]
public PartialViewResult SideNavigation()
{
    SideNavigationModel model = _sideNavigationFactory.GetSideNavigation();

    if (model != null)
    {
        return PartialView(model);
    }

    return default(PartialViewResult);
}

当我调用它时效果很好:

@Html.Action("SideNavigation", "Template") 

在我的主模板中。但是我注意到,当我更新侧面导航的 cshtml 文件时,即使在 web.config 中禁用了输出缓存,它也不会在网页上更新:

<outputCache enableOutputCache="false">

如果我更改打开的主模板,该模板会更新,但模板的导航部分不会。这是预期的行为吗?如果是这样,是否有办法仅在启用输出缓存时才输出缓存?

【问题讨论】:

  • @LaurentLequenne 无法在仅限儿童的操作中使用个人资料:stackoverflow.com/questions/4728958/…
  • 好的,如果您查看该问题的其他答案,则表示您可以使用自定义属性。我没有尝试子动作,但它在控制器上运行良好......
  • 这有同样的问题 - 不注意 web.config 中的禁用。自定义属性所做的只是从缓存配置文件中分配值
  • 但是在您的自定义属性中,您可以读取配置中的禁用值并应用或不应用缓存:-)

标签: c# asp.net-mvc outputcache


【解决方案1】:
using System.Web.Configuration;
using System.Web.Mvc;

namespace Mvc.Filters
{
    public class ExtendedOutputCacheAttribute : OutputCacheAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!web.config.caching.disabled) // just read the right config setting somewhere 
            {
               base.OnActionExecuting(filterContext);
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    感谢this answer 和 Laurent 提供的答案的混合,我使用自定义属性提出了以下解决方案:

    public class ChildActionOutputCacheAttribute : OutputCacheAttribute
    {
        private const string _cachingSection = "system.web/caching/";
        private const string _outputCacheSection = "outputCache";
        private const string _profileSection = "outputCacheSettings";
        private bool _profileEnabled;
    
        public ChildActionOutputCacheAttribute(string cacheProfile)
        {
            // get output cache section of web config
            OutputCacheSection settings = (OutputCacheSection)WebConfigurationManager.GetSection($"{_cachingSection}{_outputCacheSection}");
    
            // check section exists and caching is enabled
            if (settings != null && settings.EnableOutputCache)
            {
                // if caching enabled, get profile
                OutputCacheSettingsSection profileSettings = (OutputCacheSettingsSection)WebConfigurationManager.GetSection($"{_cachingSection}{_profileSection}");
                OutputCacheProfile profile = profileSettings.OutputCacheProfiles[cacheProfile];
    
                if (profile != null && profile.Enabled)
                {
                    // if profile exits set profile params
                    Duration = profile.Duration;
                    VaryByParam = profile.VaryByParam;
                    VaryByCustom = profile.VaryByCustom;
    
                    _profileEnabled = true;           // set profile enable to true as output cache is turned on and there is a profile
                }
            }
        }
    
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (_profileEnabled)
            {
                // only run this if caching has been set and is enabled
                base.OnActionExecuting(filterContext);
            }
        }
    }
    

    然后可以使用以下命令将其添加到子操作控制器中:

    [ChildActionOutputCache(CacheProfile.Long)]
    

    如果您的 web.config 中有以下部分:

    <system.web>
      <caching>
        <outputCache enableOutputCache="false"></outputCache>
        <outputCacheSettings>
          <outputCacheProfiles>
            <add name="Long" duration="86400" varyByParam="*" varyByHeader="none" location="Server" />
          </outputCacheProfiles>
        </outputCacheSettings>
      </caching>
    </system.web>
    

    【讨论】:

    • 您确定要重用 OutputCacheAttribute 构造函数中的完整代码吗? :-)
    • 是的 - 需要从配置文件中获取设置
    • 所以你错过了 location 属性:)
    猜你喜欢
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    相关资源
    最近更新 更多