ASP.NET MVC緩存過(guò)濾器用法
緩存過(guò)濾器用來(lái)輸出頁(yè)面緩存,其用法如下圖所示:

注意:
Duration:表示緩存多少秒;VaryByParam:表示緩存是否隨地址參數(shù)而改變。OutputCache除了可以定義在Action方法上面以外,還可以定義在控制器上面。
演示示例:
新建一個(gè)MVC應(yīng)用程序,添加一個(gè)名為Cache的控制器,Cache控制器的Index方法里面將當(dāng)前時(shí)間輸出到頁(yè)面中,Cache控制器定義如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace _2_緩存過(guò)濾器.Controllers
{
public class CacheController : Controller
{
[OutputCache(Duration =5,VaryByParam ="none")]
// GET: Cache
public ActionResult Index(int? id)
{
ViewData["CurrentTime"] = "現(xiàn)在的時(shí)間是:" + 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é)果

刷新頁(yè)面的時(shí)候,只有時(shí)間過(guò)了5秒以后,頁(yè)面上面顯示的時(shí)間才會(huì)刷新。
如果把VaryByParam的值改為id,那么在5秒的時(shí)間范圍內(nèi),頁(yè)面顯示的時(shí)間會(huì)隨著id值的改變而改變,即只要id的值改變一次,頁(yè)面顯示的時(shí)間就會(huì)改變。
在MVC程序中使用緩存過(guò)濾器的時(shí)候,由于控制器的代碼需要編譯后才能發(fā)布,在發(fā)布之后,如果要修改緩存的策略,就很麻煩,這時(shí)可以采用如下圖所示的方法,把緩存策略寫(xiě)在配置文件里面,這樣即使在程序發(fā)布之后,我們也可以隨時(shí)調(diào)整緩存的策略。

配置文件修改如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
有關(guān)如何配置 ASP.NET 應(yīng)用程序的詳細(xì)信息,請(qǐng)?jiān)L問(wèn)
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_緩存過(guò)濾器.Controllers
{
public class CacheController : Controller
{
[OutputCache(CacheProfile = "cpfile")]
// GET: Cache
public ActionResult Index(int? id)
{
ViewData["CurrentTime"] = "現(xiàn)在的時(shí)間是:" + DateTime.Now;
return View();
}
}
}運(yùn)行結(jié)果和上面的結(jié)果一樣。
到此這篇關(guān)于ASP.NET MVC緩存過(guò)濾器用法的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- ASP.NET?MVC自定義操作過(guò)濾器
- ASP.NET MVC自定義授權(quán)過(guò)濾器
- ASP.NET?MVC自定義異常過(guò)濾器使用案例
- ASP.NET MVC自定義異常過(guò)濾器
- ASP.NET?MVC授權(quán)過(guò)濾器用法
- ASP.NET MVC異常過(guò)濾器用法
- ASP.NET Core MVC中過(guò)濾器工作原理介紹
- .NET6自定義WebAPI過(guò)濾器
- ASP.NET Core MVC 過(guò)濾器(Filter)
- ASP.NET Core MVC 過(guò)濾器的使用方法介紹
- ASP.NET MVC過(guò)濾器執(zhí)行順序介紹
相關(guān)文章
文本框中輸入小寫(xiě)字母即時(shí)轉(zhuǎn)換為大寫(xiě)實(shí)現(xiàn)思路
系統(tǒng)中有一個(gè)文本框,要求輸入大寫(xiě)字母,只是用戶不是那么配合所以只好在程序來(lái)控制了,感興趣的朋友可以參考下哈2013-03-03
asp.net下 jquery jason 高效傳輸數(shù)據(jù)
jquery jason 高效傳輸數(shù)據(jù)轉(zhuǎn)自網(wǎng)上稍有修改2009-03-03
asp.net?core?MVC?全局過(guò)濾器之ExceptionFilter過(guò)濾器(1)
這篇文章主要為大家詳細(xì)介紹了asp.net?core?MVC?全局過(guò)濾器之ExceptionFilter過(guò)濾器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán)功能
這篇文章主要介紹了ASP.NET?Core?6.0?添加?JWT?認(rèn)證和授權(quán),本文將分別介紹?Authentication(認(rèn)證)?和?Authorization(授權(quán)),通過(guò)實(shí)例代碼分別介紹了這兩個(gè)功能,需要的朋友可以參考下2022-04-04
運(yùn)用.net core中實(shí)例講解RabbitMQ
RabbitMQ是實(shí)現(xiàn)了高級(jí)消息隊(duì)列協(xié)議(AMQP)的開(kāi)源消息代理軟件(亦稱面向消息的中間件),本文詳細(xì)講解了RabbitMQ以及運(yùn)用.net core中實(shí)例講解其6中模式,感興趣的小伙伴一起來(lái)學(xué)習(xí)吧2021-09-09
將FreeTextBox做成控件添加到工具箱中的具體操作方法
以下是對(duì)將FreeTextBox做成控件添加到工具箱中的具體操作方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下2013-09-09
asp.net 簡(jiǎn)便無(wú)刷新文件上傳系統(tǒng)
之前寫(xiě)過(guò)一個(gè)仿163網(wǎng)盤(pán)無(wú)刷新多文件上傳系統(tǒng),已經(jīng)對(duì)無(wú)刷新上傳文件的原理做了詳細(xì)的分析而這次的系統(tǒng)主要是針對(duì)單個(gè)file控件的,便攜版,使用更簡(jiǎn)單,還有更深入的分析2012-05-05

