-2

我怎么能在c#中做到这一点

如何在 IIS 中为应用程序设置输出缓存?以编程方式

4

1 回答 1

2

您可以在操作方法上使用 OutputCache 属性,如下所示

[OutputCache(Duration =20,VaryByParam ="ID")] 
public ActionResult SearchRecord(int ID)   

如果我们想在多个 Action 方法或多个控制器上拥有相同类型的 OutputCache 属性,那么我们可以使用 CacheProfile 属性。CacheProfile 有几个优点。例如,我们可以从一个中心位置在多个位置更改 OutputCache 属性,并且无需重新编译我们的应用程序即可应用。在 system.web 部分中添加缓存部分,其中将包含 OutputCacheSettngs。在 OutputCacheSettngs 中,我们添加了带有名称和其他属性的 outputCacheProfiles。

<caching>  
      <outputCacheSettings>  
        <outputCacheProfiles>  
          <add name="CacheFor100Seconds" duration="100" varyByParam="none" location="Server"/>  
        </outputCacheProfiles>  
      </outputCacheSettings>  
</caching>  

转到您的 Action 方法或控制器并在 OutputCache Action 中添加 CacheProfile 属性

[OutputCache(CacheProfile = "CacheFor100Seconds")]  
public ActionResult MyAction()

来自http://www.c-sharpcorner.com/article/outputcache-action-filter-in-asp-net-mvc/

于 2017-07-24T10:32:54.667 回答