MVC4在配置文件中查看缓存持续时间?

Nic*_*unt 7 asp.net caching output-caching asp.net-mvc-4

有没有在MVC4 .net页面的web.config中设置缓存的持续时间?我有 :

[OutputCache(Duration = Convert.ToInt32(ConfigurationManager.AppSettings["cache.eventPage"]), VaryByParam = "Id")]
public ActionResult....
Run Code Online (Sandbox Code Playgroud)

哪个不会编译,因为

属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式

我们有非常灵活的流量,并且希望能够在推出新版本的情况下非常快速地更改此值.这可能吗?

Ove*_*ine 15

您可以使用OutputCache配置文件 ; 在web.config中定义一个部分

<system.web>
 <caching>
  <outputCacheSettings>
    <outputCacheProfiles>
       <add name="CacheProfile1" duration="10" />  <!--10 seconds -->
       <add name="CacheProfile2" duration="3600" /> <!--one hour-->
       <add name="CacheProfileNone" duration="0" /> <!--disabled-->
    </outputCacheProfiles>
  </outputCacheSettings>
 </caching>
</system.web>
Run Code Online (Sandbox Code Playgroud)

通过您已经完成的属性在控制器操作方法上使用它.只需使用该CacheProfile物业.

[OutputCache(CacheProfile = "CacheProfile1",VaryByParam = "Id")]
Run Code Online (Sandbox Code Playgroud)

您可以为每个缓存方案创建不同的配置文件.

有关MSDN缓存的更多信息