Chad Moran
Chad Moran

Reputation: 12854

Ways to workaround whole page caching in ASP.NET MVC

Currently ASP.NET MVC's OutputCache attribute has a huge downfall. If you want to cache parts of your site you have to use a workaround due to the limitation of ASP.NET's pipeline that MVC relies on.

Say you have a page that has a statistics module that you surface through RenderAction you can't cache just that part of the page out of the box.

My question is, what ways have you found to get around this limitation that are elegant and easy to use? I've personally found 2 of them neither I'm particularly happy with. Though they work they seem to just feel wrong when building an app around them.

Solution 1 - Sub Controllers http://mhinze.com/subcontrollers-in-aspnet-mvc/

Solution 2 - Partial requests http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/

So if you have another solution or maybe even a way you've used one of these solutions elegantly I'd love some ideas on design and/or usage.

Upvotes: 4

Views: 1425

Answers (3)

JC Grubbs
JC Grubbs

Reputation: 40311

I've done this with option 2 (using Html.RenderAction) with pretty good success. I also created different base classes for my controllers, one that caches and one that doesn't so that I put all of my cached actions in one place. I don't do this very often so it's not too bad to isolate these actions. With a combination of caching and a GZip compression filter I wrote I get pretty blazing performance out of MVC.

Upvotes: 4

eu-ge-ne
eu-ge-ne

Reputation: 28153

In some cases it's better not to use ASP.NET OutputCache feature at all. Instead use Caching in your business/service layer with optional gzip compression. Sometimes this combination is even faster than full output caching.

Upvotes: 4

Marc Gravell
Marc Gravell

Reputation: 1062865

How about using jQuery to load some of the areas (divs etc) via ajax. Then you can cache different areas as complete requests, with different granularity.

Upvotes: 1

Related Questions