MVC站点中从配置文件中获取OutputCache的相关配置

在MVC的站点中使用OutputCache实现缓存可以提高页面的打开速度,如:

#if !DEBUG
        [OutputCache(VaryByHeader = "Content-Type", Duration = 600)]
#endif
        public ActionResult Index()
        {
             return View();
        }    

 

如果每次想要更改缓存时间的话,则需要重新编译,但是可以通过在Web.Config添加配置节点来实现不编译更改缓存配置

在<system.web>节点中添加如下配置

<caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <add name="IndexOutputCache" duration="600" varyByHeader="Content-Type"/>
          <add name="ArticleListPageOutputCache" duration ="600" varyByHeader="Content-Type"/>
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>

之后再C#代码中

#if !DEBUG
        [OutputCache(CacheProfile="IndexOutputCache")]
#endif
        public ActionResult Index()
        {
             return View();
        }

 

posted @ 2013-04-19 11:19  DCLancer  阅读(250)  评论(0编辑  收藏  举报
(function() { var c = document.createElement('script'); c.type = 'text/javascript'; c.async = true; c.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'www.clicki.cn/boot/48212'; var h = document.getElementsByTagName('script')[0]; h.parentNode.insertBefore(c, h); })();