MVC页面缓存
1.OutputCache 属性 contact.cshtml
[OutputCache(Duration=10)]
public ActionResult Contact()
{ ViewBag.Message ="Your contact page."; returnView(); } action 结果会缓存10秒钟
2.带参数的OutputCache
[OutputCache(Duration=3600, VaryByParam="searchTerm")]publicActionResult Contact(stringsearchTerm)
3.缓存child action partialviewtest.cshtml
<p> This is a partial view. </p>
publicActionResult PartialViewTest(){ returnView();} contact.cshtml中,加入如下代码@Html.Action("PartialViewTest") 在partialviewtest action上加入[OutputCache(Duration=3600)]设置,partialviewtest的缓存时间不会受contact action缓存时间的影响 4 指定缓存位置Specify the cache location [OutputCache(Duration = 3600, Location=System.Web.UI.OutputCacheLocation.ServerAndClient)] 默认值是serverandclient 5.根据header值进行缓存 例如多区域语言情况下 [OutputCache(Duration = 3600, VaryByHeader="Accept-Language")],可以根据不同的语言进行缓存。
other type of header value will be important for AJAX requests. Let’s imagine that the Contact action is also available to AJAX calls. In other words only that portion of the screen will be refreshed which makes up the View result of the Contact action. The rest, i.e. the header, footer, menu etc. will stay constant. If a user then bookmarks a specific search result, e.g.:
http://localhost:xxxx/Home/Contact?searchTerm=a
…and will call this search directly from his/her browser then the Contact action will only serve up a cached version of the Contact view and nothing else. The reason is that user’s browser will send a full GET request and not an asynchronous one. All the other parts of the ‘normal’ full screen, i.e. everything that the AJAX call didn’t touch will be left out. The result will be very weird: a page devoid of styles – apart from any inline style tags in the Contact view – headers, footers etc. The problem lies with a specific header value: X-Requested-With. This is the only difference between the headers of the AJAX and the GET calls: it will be present on an AJAX request but absent on GET requests. To the caching engine there’s no difference between AJAX calls and GET calls because you didn’t specify this in the OutputCache attribute. Two things need to be done:
1. Update the OutputCache attribute to vary the output by this specific header type:
|
1
|
[OutputCache(Duration = 3600, VaryByHeader="X-Requested-With")] |
Now the caching mechanism will be able to differentiate between two requests: one that wants a full page render and one that will only serve a portion of the page.
Unfortunately there are cases when this update is not enough. The default setting is that we’re letting the action result be cached on both the client and the server. However, some older browsers are not “clever” enough to detect the difference between full GET and partial AJAX requests.
2. To be on the safe side we need to specify that caching only should occur on the server:
You can achieve this as follows:
|
1
|
[OutputCache(Duration = 3600, VaryByHeader="X-Requested-With", Location=System.Web.UI.OutputCacheLocation.Server)] |
Now the server will send instructions to the browser not to cache the action result. The browser will always have to access the server for a response. The server will then serve up a cached result if there’s any in its memory.
The safest and most elegant solution is of course to create separate action methods for full GET and partial AJAX calls
<caching> <outputCacheSettings> <outputCacheProfiles> <addname="Aggressive"duration="1600"/> <addname="Short"duration="20"/> </outputCacheProfiles> </outputCacheSettings></caching>可以这样使用: [OutputCache(CacheProfile="Aggressive")]MVC页面缓存的更多相关文章
- Mvc 页面缓存 OutputCache VaryByCustom
优化网站,dotNet MVC 可以通过(OutputCache)特性在某些Action上使用缓存,如果我们想要自定义缓存依据可以通过如下方式进行: 第一步, 在 global.asax.cs 文件中 ...
- asp.net mvc 页面缓存
在任务中需要实现点击浏览器back按钮,加载的前一页面需要强制刷新. 想要在前端通过js来绑定数据实现,但是觉得太麻烦,还是用另一种方式来解决: 不缓存该页面. 简单易懂: Response.Cach ...
- [转]Asp.net mvc 网站之速度优化 -- 页面缓存
网站速度优化的一般方法 由于网站最重要的用户体验就是速度,特别是对于电子商务网站而言. 一般网站速度优化会涉及到几个方面: 1. 数据库优化 — 查询字段简历索引,使用数据库连接池和持久化,现在还有种 ...
- Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列
- 在ASP.NET MVC 3 中自定义AuthorizeAttribute时需要注意的页面缓存问题
一.ASP.NET MVC中使用OutputCache实现服务器端页面级缓存 在ASP.NET MVC中,假如我们想要将某个页面(即某个Action)缓存在服务器端,可以在Action上标上以下特性: ...
- 实现asp.net mvc页面二级缓存,提高访问性能
实现的mvc二级缓存的类 //Asp.Net MVC视图页面二级缓存 public class TwoLevelViewCache : IViewLocationCache { private rea ...
- Asp.net mvc 网站之速度优化 -- 页面缓存
网站速度优化的一般方法 由于网站最重要的用户体验就是速度,特别是对于电子商务网站而言. 一般网站速度优化会涉及到几个方面: 1. 数据库优化 — 查询字段简历索引,使用数据库连接池和持久化,现在还有种 ...
- ASP.NET MVC 阻止当前请求的视图页面缓存OutputCache
设置缓存 [OutputCache(Duration =333,VaryByCustom ="Index")] 缓存: //在action中,临时阻止该次请求的视图页面缓存 Res ...
- 探索ASP.NET MVC5系列之~~~5.缓存篇(页面缓存+二级缓存)
其实任何资料里面的任何知识点都无所谓,都是不重要的,重要的是学习方法,自行摸索的过程(不妥之处欢迎指正) 汇总:http://www.cnblogs.com/dunitian/p/4822808.ht ...
随机推荐
- CS中窗体的基类(BaseForm)注意点
窗体基类最好新建一个窗体(BaseForm) 1.这样能够保证在VS中保证他的派生窗口也能够可视化. 2.如果基类直接是一个cs类文件,对于处理派生窗口就很复杂,比如按钮权限之类的操作; 如果直接继承 ...
- MongoDB Windowns 配置使用
MongoDB 下载 MongoDB 提供了可用于 32 位和 64 位系统的预编译二进制包,你可以从MongoDB官网下载安装,MongoDB 预编译二进制包下载地址:https://www.mo ...
- Xamarin.Forms第三方XAML预览工具-LiveXAML简单体验
截至目前,Xamarin官方的Xaml Previewer工具仍然处于测试阶段,使用中也发现了各种不便,例如各种莫名其妙的渲染失败,或者提示需要编译项目才能渲染等等,复杂项目基本不可用, 完全没有体现 ...
- C#构造方法(函数)
一.概括 1.通常创建一个对象的方法如图: 通过 Student tom = new Student(); 创建tom对象,这种创建实例的形式被称为构造方法. 简述:用来初始化对象的,为类的成员赋值 ...
- 【cocos2d-x 手游研发----地图活起来了】
谈到地图不少人都说要做地图编辑器了,但是我暂时绕过这一步,如果不用寻路地图就不能移动?寻路就是会绕过障碍物的算法. 我做了一个简单的地图的思想,就是地图分层3层:背景层.可行区域层.遮罩层,但是地图就 ...
- cpu缓存java性能问题初探
在内存与cpu寄存器之间,还有一块区域叫做cpu高速缓存,即我们常常说的cache. cache分为L1.L2.L3三级缓存,速度递减,离cpu越来越远,L1.L2每个内核自己都有,L3是每个插槽上的 ...
- 程序媛计划——python初级课时3~5
产生1-10中的随机数: for 循环:所有可遍历对象都能用于for循环,如一个字符串. len(list),list中的元素类型可以各不相同:可以直接用下标对list元素赋值来更新列表 对字符串可以 ...
- Django安装(第一个项目)
day60 从系统中选择已存在的解释器. 新建Django项目 命令行创建: django-admin startproject mysite ...
- java-斐波那契数列的解法
public class Feibo { static long[] temp = new long[1000000]; static long fun1(int n){ if(temp[n]!=0) ...
- 萝卜保卫战3内购破解+Toast窗口增加(Love版)
涉及到一些不同的破解的方法,以及不同的破解思路,还有一些重要权限的删除等. 作者:HAI_ 这次目标是经常玩的萝卜保卫战,不知不觉,已经更新到3了.详细分析请参考https://bbs.ichunqi ...