Asp.net

MVC4在配置文件中查看記憶體持續時間?

  • April 24, 2017

是否可以在 web.config 中為 MVC4 .net 頁面設置記憶體的持續時間?我有 :

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

哪個不會編譯,因為

屬性參數必須是屬性參數類型的常量表達式、typeof 表達式或數組創建表達式

我們的流量非常大,希望能夠在不推出新版本的情況下快速更改此值。這可能嗎?

您可以使用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 上有關記憶體的更多資訊

引用自:https://stackoverflow.com/questions/17433408