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();

        }

 

你可能感兴趣的:(cache)