加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 04:17:31 所属栏目:asp.Net 来源:网络整理
导读:是否有一个在MVC4 .net页面的web.config中设置缓存的持续时间?我有 : [OutputCache(Duration = Convert.ToInt32(ConfigurationManager.AppSettings["cache.eventPage"]),VaryByParam = "Id")]public ActionResult.... 哪个不会编译,因为 An attribute argu
是否有一个在MVC4 .net页面的web.config中设置缓存的持续时间?我有 :
[OutputCache(Duration = Convert.ToInt32(ConfigurationManager.AppSettings["cache.eventPage"]),VaryByParam = "Id")]
public ActionResult....

哪个不会编译,因为

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

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

解决方法

你可以用 OutputCache profiles;在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")]

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

More info on caching at MSDN

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读