当前位置 > 首页 > Asp.net

Output caching with ASP.NET MVC Razor

2010-12-3 19:59:00来源:Asp.net

Caching data greatly increase the website performance as its not going to do server round trip. I have already mentioned how you can use Output caching in web forms in earlier blog post here. Let’s see how we can do same thing with asp.net mvc. For this example I have used asp.net mvc razor. In asp.net mvc you can use OutputCache attribute to cache the output. Just like below.

[OutputCache(VaryByParam="none",Duration=60)]
public ActionResult Index()
{
ViewModel.Message = DateTime.Now.ToString();
return View();
}

Here it will cache the view for 60 second and will not go for server round trip. Let’s see How it will look into the browser.

Temp

You can also set the output caching in web.config and and create output cache profile which you can use any where like following.

<caching>
<outputCacheSettings>
<outputCacheProfiles>
<clear/>
<add
name="MyOuputCacheProfile"
duration="60"
varyByParam="none" />
</outputCacheProfiles>
</outputCacheSettings>
</caching>

Here how you can use that profile.

 [OutputCache(CacheProfile = "MyOuputCacheProfile")]
public ActionResult Index()
{
ViewModel.Message = DateTime.Now.ToString();
return View();
}

It support four type of settings for output caching. VaryByContentEncoding, VaryByParam, VaryByCustom,VaryByHeader. Hope this will help you!! happy Programming.

Technorati Tags: ASP.NET MVC,Razor,OutputCache
Shout it


  • 为ASP.NET MVC创建一个基于Mini容器的ControllerFactory

    2011-12-19 13:38:00

    背景 随着Asp.net mvc的越来越流行,关于和Mvc整合的DI容器也非常的多,而Mini容器一致没有把整合的方案发布出来,以至于不时的收到有些网友询问这些事,于是便有了该文。[ 源代

  • C++代码覆盖率工具Coverage Validator

    2009-7-12 1:12:00

    市面上的C++代码覆盖率工具大都收费,Coverage Validator也不例外。Coverage Validator应该少有人听过,我也是在 stackoverflow 里听别人介绍的。所以下载

  • 从央视网银曝光到微软大中华区正版推进战略

    2009-3-21 11:58:00

    摘要: 近些天总听朋友们在聊天中提到央视曝光网银账户被盗的相关话题,我自己并没有看电视,但也甚至安全问题是一个对于普通计算机用户最担心和头疼的问题。借此也令我萌发出来一些想法:为什么央视在曝光网银被

  • 页面静态化在MOSS中的应用

    2009-9-2 20:36:00

    摘要: 页面静态化在MOSS中的应用今天公司有这么一个计划,想用MOSS搭建内部的OA,包括发布网站,然后把发布网站静态化一下,上传到购买的空间里,作为公司对外的门户。起到一个宣传的作用,所以就找了

  • List的Sort()排序的疑问

    2007-4-23 13:05:00

    我们看下List的Sort排序的函数为 public void Sort ( Comparison <T> comparison ) This me

  • .NET简谈互操作(七:数据封送之介绍)

    2011-7-5 13:46:00

    互操作系列文章: .NET简谈互操作(一:开篇介绍) .NET简谈互操作(二:先睹为快) .NET简谈互操作(三:基础知识之DllImpo

  • Customize the SimpleMembership in ASP.NET MVC 4.0

    2012-11-15 12:29:00

    As we know, .NET 4.5 have come up to us, and come along with a lot of new interesting features as w

  • Reordering invoice lines using jqGrid and TableDND extension

    2009-7-22 20:55:00

    In one of my ASP.NET MVC applications I needed flexible interface for inserting invoice lines. Some

  • NetSnmp初步(三):接收控制命令:实现SNMP的SET命令

    2012-3-30 14:27:00

    主要是接收并处理snmp客户端发送控制命令:启动/停止单个节目的录制。也就是实现snmp的SET命令。 1.还是从MIB开始。 由于只是一个简单的起停命令,前面定义的recordStatusTab

  • 新浪微博SDK for .Net 4.0第二版Winform DEMO发布了。

    2012-5-29 17:19:00

    首先感谢大家支持新浪微博SDK for .Net 4.0项目! 在这个百无聊赖的下午,我编写了一个简单的微博客户端,实现了微博几个最基本的功能供大家参考。 运行时截图 登录界面 功