当前位置 > 首页 > 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 2.0中操作数据

    2007-4-16 6:54:00

    【原文地址】 Working with Data in ASP.NET 2.0 【原文发表日期】 Saturday, April 14, 2007 11:08 AM 最近,有一堆关于如何在AS

  • Go语言先睹为快

    2009-11-17 0:15:00

    阅读: 431 评论: 7 作者: Bill New 发表于 2009-11-16 21:22 原文链接 .codestart{ background:url(htt

  • ActiveRecord自动维护双向关联的测试

    2007-4-15 10:06:00

    摘要: Trunk版本的AR使用的NHibernate版本为 1.2 beta,这个版本添加了对泛型的支持。而之前我们要在AR中使用泛型(强类型集合),主要是依靠NHIbernate.G

  • Bringing true agility to enterprise .NET: Tellago Studios announces TeleSharp

    2011-6-30 21:57:00

    We are happy to announce the latest addition to Tellago Studios’ product family: TeleSharp . After

  • MMIX机器简要介绍

    2009-12-6 23:03:00

    阅读: 457 评论: 2 作者: John Waken 发表于 2009-12-06 16:36 原文链接 学计算机的应该听过Don Knuth的大名吧,他的七卷本著作《the Art o

  • 谈 三层结构与MVC模式的区别

    2009-4-29 23:45:00

    在CSDN和园子里有朋友谈到三层与MVC的区别,以前也有人抛出这个问题,本人对来公司面试的朋友也偶乐会提这方面的问题。 那么我也来讲讲我对这两者的理解吧。 首先对这个题目,本身是存在问题

  • ASP.NET 4.0: Modifying ClientID of controls

    2009-5-31 6:01:00

    Client-side ID-s of ASP.NET controls have been problem for a long time. They are hard to predict an

  • IIS URL Rewriting 和 ASP.NET routing(下)

    2010-1-5 17:23:00

    阅读: 833 评论: 2 作者: LanceZhang 发表于 2010-01-05 09:45 原文链接 在 IIS URL Rewriting 和 ASP.NET routing(上

  • TheBeerHouse(2):web.config的魅力

    2007-5-12 14:47:00

    摘要: 这个跟以前~~介绍过的Web设置 类似,只是这里的Web设置更好点- -先看下web.config片断 <configurationxmlns=http://schemas.micro

  • Cortex BDD Test Framework

    2010-2-20 14:21:00

    Back in 2008 I joined Cortex . I was lucky to join a dynamic and growing development team, not afr