OutputCache in Razor.cshtml Viewer

Using ASP.NET MVC web forms, we can place the output cache either at the controller level or at the presentation level. How can we specify "outputcache" on the .cshtml page?

I did not find anything. Where can I get the syntax?

+6
asp.net-mvc asp.net-mvc-3 razor outputcache
source share
4 answers

What does ASP.NET MVC Web Forms mean? If you reference the OutputCache attribute in the Page directive, these are ASP.NET web forms.

ASP.NET MVC has output caching at the controller action level:

  [OutputCache(Duration=10, VaryByParam="none")] public ActionResult Index() { return View(); } 

This is independent of the viewing engine (ASPX / Razor).

+7
source share

Using ASP.NET MVC web forms, you can put the output cache at the presentation level, but this will not affect. It is there because it is a legacy from classic ASP.NET. In ASP.NET MVC, the output cache must always be placed in the controller action.

Since the value of the cache in the view does not make sense in the recently appeared Razor viewer, there is no such possibility. You should always put this attribute in a controller action.

+2
source share

See Master Gu's latest post on this topic: MVC2 Announcement

Partially this part:

Output Caching Enhancements

The ASP.NET MVC 3s output caching system no longer needs to specify the VaryByParam property when declaring [OutputCache] on the Controller action method. MVC3 now automatically changes the output write cache when you have the parameters of your action method - allowing you to purely allow output caching of actions using the code below:

alt text

In addition to supporting full page output caching, ASP.NET MVC 3 also supports partial page caching - it allows you to cache the output area and reuse it on multiple requests or controllers. [OutputCache] the behavior for partial caching has been updated using RC2 so that the sub-content of the cache entries varies depending on the input parameters as opposed to the URL structure of the top-level request - which makes caching scripts both simpler and more powerful than the behavior in the previous RC

Thus, it improves a lot for us.

  • By simply pointing the OutputCache to a controller action, it will take care of cashing the result from that particular action for a certain duration. The cache will automatically change using certain action parameters (which is usually the desired behavior.)
  • It will also work transparently on child actions (those that are invoked through Html.Action (...))
+2
source share

It seems that others have answered the main question, which is: do not configure page caching in the page / cshtml file in MVC3 +, use the Action method in the controller.

However, for more complex scenarios, you can access the WebCache object using the Razor syntax.

Some of these scenarios are the old Donut / Donut (or Donut / Dounut) caching. The MVC3 theme is focused here on Stack Overflow .

Also found is the NuGet MvcDonutCaching package mentioned by Denis Huvelle that solves the problem for 3 and 4 - but I have not tested it.

+1
source share

All Articles