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?
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?
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).
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.
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:
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.
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.
© 2022 - 2024 — McMap. All rights reserved.