OutputCache in Razor view engine .cshtml page
Asked Answered
S

4

7

Using ASP.NET MVC web forms we can put output cache either in controller level or view level. How can we mention "outputcache" in .cshtml page?

I did not find it anywhere. Where can I get the syntax?

Stipule answered 11/12, 2010 at 6:34 Comment(1)
Hello Micheal; Did you find any answer yourself?Pyrrolidine
L
7

What do you mean "ASP.NET MVC Web Forms"? If you're referring to the OutputCache attribute in the Page directive, that is ASP.NET Web Forms.

ASP.NET MVC has Output Caching on the controller action level:

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

This is irrespective of the view engine (ASPX/Razor).

Lubricant answered 11/12, 2010 at 6:51 Comment(3)
Sorry, I should have mentioned it as web forms engine.Stipule
What do you mean "web forms engine"? Are you using ASP.NET MVC or ASP.NET Web Forms? Web Forms = OutputCache directive in ASPX page directive. MVC = OutputCache attribute on controller action.Lubricant
You can also set it in the view: haacked.com/archive/2009/05/12/donut-hole-caching.aspx But how do you do this with the razor viewengine?Serpentiform
M
2

Using ASP.NET MVC web forms you can can put output cache on the view level but this wouldn't have effect. It's there because it's an heritage from classic ASP.NET. In ASP.NET MVC the output cache should always be placed on the controller action.

Because putting cache values in the view makes no sense in the newly introduced Razor view engine there's not such possibility. You should always put this attribute on the controller action.

Mignon answered 11/12, 2010 at 10:43 Comment(2)
Phil Haacked seems to disagree: haacked.com/archive/2009/05/12/donut-hole-caching.aspxSerpentiform
@DePeter But how do you do that in razor?Hushaby
S
2

Refer to the latest post by Master Gu on this subject: MVC2 Announcement

Particulary this part:

Output Caching Improvements

ASP.NET MVC 3’s output caching system no longer requires you to specify a VaryByParam property when declaring an [OutputCache] attribute on a Controller action method. MVC3 now automatically varies the output cached entries when you have explicit parameters on your action method – allowing you to cleanly enable output caching on actions using code like below:

alt text

In addition to supporting full page output caching, ASP.NET MVC 3 also supports partial-page caching – which allows you to cache a region of output and re-use it across multiple requests or controllers. The [OutputCache] behavior for partial-page caching was updated with RC2 so that sub-content cached entries are varied based on input parameters as opposed to the URL structure of the top-level request – which makes caching scenarios both easier and more powerful than the behavior in the previous RC.

So this improves things a lot for us.

  1. Simply mentioning OutputCache on a controller action will take care of cashing the result from that particular Action for the defined duration. The cache will automatically be varied by the defined action parameters (which is normally the desired behavior.)
  2. It will also work transparently on Child Actions (the ones invoked via Html.Action(...))
Shantung answered 12/12, 2010 at 20:37 Comment(2)
What is solution your sample -Browse method- if implement thread level parameters? Forexample; method can have a inline parameter as string lang =Thread.CurrentThread.CurrentUICulture.cultureInfo.DisplayName and this parameter no sense for method sign (because it has only one parameter as name category, but result depends on culture.Pyrrolidine
this does not answer the question, only describes some parts of caching.Politick
V
1

Sounds as though others have answered the main question which is - do not configure page caching on the page / cshtml file in MVC3+, use the Action method in the controller.

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

Some of those scenarios are the old Doughnut / Doughnut (or Donut / Dounut) caching. An MVC3 focused thread here on Stack Overflow.

Also found a NuGet package MvcDonutCaching mentioned by Denis Huvelle which solves the problem for 3 & 4 - but I haven't tested it.

Vocable answered 25/1, 2013 at 10:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.