7

Is there a was to set the duration of caching in the web.config for MVC4 .net pages? I have :

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

Which will not compile because

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

We have very spikey traffic and would like to be able to change this value very quickly with out pushing out a new build. Is this possible?

4

1 回答 1

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>

正如您已经完成的那样,通过属性在您的控制器操作方法上使用它。只需使用该CacheProfile属性。

[OutputCache(CacheProfile = "CacheProfile1",VaryByParam = "Id")]

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

MSDN 上有关缓存的更多信息

于 2013-07-03T03:23:47.587 回答