ASP.NET MVC緩存過濾器用法
緩存過濾器用來輸出頁面緩存,其用法如下圖所示:
注意:
Duration:表示緩存多少秒;VaryByParam:表示緩存是否隨地址參數(shù)而改變。OutputCache除了可以定義在Action方法上面以外,還可以定義在控制器上面。
演示示例:
新建一個MVC應(yīng)用程序,添加一個名為Cache的控制器,Cache控制器的Index方法里面將當(dāng)前時間輸出到頁面中,Cache控制器定義如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace _2_緩存過濾器.Controllers { public class CacheController : Controller { [OutputCache(Duration =5,VaryByParam ="none")] // GET: Cache public ActionResult Index(int? id) { ViewData["CurrentTime"] = "現(xiàn)在的時間是:" + DateTime.Now; return View(); } } }
2、Cache控制器的Index視圖定義如下:
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> </head> <body> <div> <h2>@ViewData["CurrentTime"]</h2> </div> </body> </html>
3、程序運(yùn)行結(jié)果
刷新頁面的時候,只有時間過了5秒以后,頁面上面顯示的時間才會刷新。
如果把VaryByParam的值改為id,那么在5秒的時間范圍內(nèi),頁面顯示的時間會隨著id值的改變而改變,即只要id的值改變一次,頁面顯示的時間就會改變。
在MVC程序中使用緩存過濾器的時候,由于控制器的代碼需要編譯后才能發(fā)布,在發(fā)布之后,如果要修改緩存的策略,就很麻煩,這時可以采用如下圖所示的方法,把緩存策略寫在配置文件里面,這樣即使在程序發(fā)布之后,我們也可以隨時調(diào)整緩存的策略。
配置文件修改如下:
<?xml version="1.0" encoding="utf-8"?> <!-- 有關(guān)如何配置 ASP.NET 應(yīng)用程序的詳細(xì)信息,請?jiān)L問 https://go.microsoft.com/fwlink/?LinkId=301880 --> <configuration> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.6.1" /> <httpRuntime targetFramework="4.6.1" /> <httpModules> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> </httpModules> <!--緩存策略--> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="cpfile" duration="5" varyByParam="none"/> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> </assemblyBinding> </runtime> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules> <remove name="ApplicationInsightsWebTracking" /> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> </modules> </system.webServer> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> </configuration>
程序代碼修改如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace _2_緩存過濾器.Controllers { public class CacheController : Controller { [OutputCache(CacheProfile = "cpfile")] // GET: Cache public ActionResult Index(int? id) { ViewData["CurrentTime"] = "現(xiàn)在的時間是:" + DateTime.Now; return View(); } } }
運(yùn)行結(jié)果和上面的結(jié)果一樣。
到此這篇關(guān)于ASP.NET MVC緩存過濾器用法的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET2.0數(shù)據(jù)庫入門之SQL Server
ASP.NET2.0數(shù)據(jù)庫入門之SQL Server...2006-09-09- 就是有時候窗口不能夠成功置頂,這時需要重新切換下標(biāo)簽,就可以置頂了,本文介紹C# SetWindowPos實(shí)現(xiàn)窗口置頂?shù)姆椒?/div> 2012-12-12
ASP.NET中Application和Cache的區(qū)別分析
在asp.net中儲存數(shù)據(jù)的方式有很多,包括application,session,cache, cookie, viewstate。其中application和cache的應(yīng)用范圍,使用方式都比較相似,這里主要對比一下這兩種方式。2010-03-03ASP.NET?Core中使用Redis實(shí)現(xiàn)緩存
本文詳細(xì)講解了ASP.NET?Core中使用Redis實(shí)現(xiàn)緩存的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03[Asp.Net MVC4]驗(yàn)證用戶登錄實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了[Asp.Net MVC4]驗(yàn)證用戶登錄實(shí)現(xiàn)實(shí)例,這里整理了詳細(xì)的代碼,具有一定的參考價值,有需要的小伙伴可以參考下。2016-12-12ASP.NET頁面借助IFrame提交表單數(shù)據(jù)所遇到問題的解決方法分享
ASP.NET頁面借助IFrame提交表單數(shù)據(jù)所遇到問題的解決方法分享,碰到同樣問題的朋友可以參考下。2011-10-10在ASP.NET?MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù)的解決方法
有時候,當(dāng)用戶請求一個Controller下的Action,我們希望,在單位時間間隔內(nèi),比如每秒,每分鐘,每小時,每天,每星期,限制同一個IP地址對某個Action的請求次數(shù),如何做呢?這篇文章主要介紹了在ASP.NET?MVC下限制同一個IP地址單位時間間隔內(nèi)的請求次數(shù),需要的朋友可以參考下2024-01-01最新評論