【问题标题】:What is the default Duration of asp.net MVC OutputCache attribute?asp.net MVC OutputCache 属性的默认持续时间是多少?
【发布时间】:2016-04-22 07:02:00
【问题描述】:

我们使用的是MVC outputcache属性,如下图

[OutputCache(Location = System.Web.UI.OutputCacheLocation.Server, Duration = 60 * 60 * 12, VaryByParam = "staticDataTypes;customerSubscriptionId")]

Duration的默认值是多少?

【问题讨论】:

  • 持续时间的默认值为“0”
  • 那么如果 Duration 值为 0,就好像我们没有缓存?
  • 是的,就是这样。 0 表示不缓存。

标签: asp.net-mvc caching duration outputcache


【解决方案1】:

Duration属性在System.Web.Configuration.OutputCacheProfile.cs中初始化,相关代码如下:

_propDuration = new ConfigurationProperty("duration", typeof(int), -1, 
                                          ConfigurationPropertyOptions.None); 

和

[ConfigurationProperty("duration", DefaultValue = -1)]
public int Duration {
    get { 
         return (int)base[_propDuration];
    } 
    set { 
        base[_propDuration] = value;
    } 
}

这会将其设置为默认值 -1,这是一个无效值。 Duration 属性的文档提到:“必须在配置文件或使用配置文件的页面的指令中定义持续时间。”

所以,实际上没有(有效的)默认值,您需要指定它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-28
    • 2015-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-03
    • 2015-06-12
    • 2022-11-11
    相关资源
    最近更新 更多