ChildActionOnly 输出缓存未禁用
ChildActionOnly output cache not disabled
我在标有 [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">
如果我更改打开的主模板,主模板会更新,但模板的导航部分不会。这是预期的行为吗?如果是这样,有没有办法仅在启用输出缓存时输出缓存?
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);
}
}
}
}
多亏了 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>
我在标有 [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">
如果我更改打开的主模板,主模板会更新,但模板的导航部分不会。这是预期的行为吗?如果是这样,有没有办法仅在启用输出缓存时输出缓存?
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);
}
}
}
}
多亏了 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>