<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/creating-an-asp-net-mvc-outputcache-actionfilterattribute-sujvthdg5" -->

---
title: Creating an ASP.NET MVC OutputCache ActionFilterAttribute
description: A walkthrough shows how to build a custom OutputCache ActionFilterAttribute for ASP.NET MVC (preview 3), since the classic OutputCache directive doesn't work...
canonical: https://daily.dev/posts/creating-an-asp-net-mvc-outputcache-actionfilterattribute-sujvthdg5
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Creating an ASP.NET MVC OutputCache ActionFilterAttribute | daily.dev
og:description: A walkthrough shows how to build a custom OutputCache ActionFilterAttribute for ASP.NET MVC (preview 3), since the classic OutputCache directive doesn't work...
og:url: https://daily.dev/posts/creating-an-asp-net-mvc-outputcache-actionfilterattribute-sujvthdg5
og:image: https://api.daily.dev/og/posts/suJvtHdG5.png
og:image:alt: Creating an ASP.NET MVC OutputCache ActionFilterAttribute
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Creating an ASP.NET MVC OutputCache ActionFilterAttribute

**[Maarten Balliauw](https://daily.dev/sources/maartenballiauw)** · 5 min read · 0 upvotes · 0 comments

## Summary

A walkthrough shows how to build a custom OutputCache ActionFilterAttribute for ASP.NET MVC (preview 3), since the classic OutputCache directive doesn't work in MVC views. It covers client-side caching via HTTP cache headers and server-side caching by swapping the HttpContext with a StringWriter-backed response, storing rendered output keyed by controller, action, and VaryByParam values. Full source code is provided as a downloadable sample.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://blog.maartenballiauw.be/posts/2008-06-26-creating-an-aspnet-mvc-outputcache-actionfilterattribute>

## Questions this post answers

### How do I implement output caching in ASP.NET MVC since the OutputCache directive doesn't work in views?

Create a custom ActionFilterAttribute that overrides OnActionExecuted for client-side caching (setting HTTP cache headers like SetCacheability, SetExpires, and SetMaxAge) and OnResultExecuting/OnResultExecuted for server-side caching, which swaps the HttpContext with a StringWriter-backed response to capture and store rendered HTML in the Cache object keyed by controller, action, and parameters.

_Browsing daily.dev surfaces patterns like this when wiring up caching in older ASP.NET MVC codebases._

### How can I vary cached output by request parameters in a custom ASP.NET MVC output cache implementation?

Generate a cache key that combines the controller's full type name, the action name from route data, and each route parameter matching a VaryByParam list (or all parameters when VaryByParam is set to "*"). This produces distinct cache entries per parameter combination, mirroring the VaryByParam behavior of the classic ASP.NET OutputCache directive.

_Developers comparing caching strategies often track implementation details like this through daily.dev._

## Similar posts on daily.dev

- [Implementing RFC-compliant HTTP caching for HttpClient in .NET](https://daily.dev/posts/implementing-rfc-compliant-http-caching-for-httpclient-in-net-ckf4syuwc) · Meziantou · 5 upvotes · 1 comments
- [ASP.NET Core Controllers: A Practical Guide to Building REST Endpoints](https://daily.dev/posts/asp-net-core-controllers-a-practical-guide-to-building-rest-endpoints-qbrdx5rqi) · We Are .NET · 0 upvotes · 0 comments
- [The silent filter: How an ASP.NET MVC quirk became a security leak](https://daily.dev/posts/the-silent-filter-how-an-asp-net-mvc-quirk-became-a-security-leak-wec0w8625) · The Art of Simplicity · 2 upvotes · 0 comments
- [Api Key Authentication in ASP.NET Core](https://daily.dev/posts/api-key-authentication-in-asp-net-core-5jr6dwwxq) · We Are .NET · 3 upvotes · 0 comments

---

Tags: [#c#](https://daily.dev/tags/c#)

[View this post on daily.dev](https://daily.dev/posts/creating-an-asp-net-mvc-outputcache-actionfilterattribute-sujvthdg5)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Creating an ASP.NET MVC OutputCache ActionFilterAttribute","url":"https://daily.dev/posts/creating-an-asp-net-mvc-outputcache-actionfilterattribute-sujvthdg5","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/creating-an-asp-net-mvc-outputcache-actionfilterattribute-sujvthdg5"},"datePublished":"2026-10-04T13:06:13.515Z","dateModified":"2026-10-04T13:16:38.977Z","description":"A walkthrough shows how to build a custom OutputCache ActionFilterAttribute for ASP.NET MVC (preview 3), since the classic OutputCache directive doesn't work...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5dff34ac40ae566c5b529bdb553eeea6?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5dff34ac40ae566c5b529bdb553eeea6?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Maarten Balliauw","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Maarten Balliauw","logo":"https://media.daily.dev/image/upload/s--rOTg3VYO--/f_auto,q_auto/v1791119043/logos/maartenballiauw?_a=BAMAMicg0","url":"https://daily.dev/sources/maartenballiauw"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/creating-an-asp-net-mvc-outputcache-actionfilterattribute-sujvthdg5","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"c#","timeRequired":"PT5M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Maarten Balliauw","item":"https://daily.dev/sources/maartenballiauw"},{"@type":"ListItem","position":3,"name":"Creating an ASP.NET MVC OutputCache ActionFilterAttribute"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/creating-an-asp-net-mvc-outputcache-actionfilterattribute-sujvthdg5#faq","mainEntity":[{"@type":"Question","name":"How do I implement output caching in ASP.NET MVC since the OutputCache directive doesn't work in views?","acceptedAnswer":{"@type":"Answer","text":"Create a custom ActionFilterAttribute that overrides OnActionExecuted for client-side caching (setting HTTP cache headers like SetCacheability, SetExpires, and SetMaxAge) and OnResultExecuting/OnResultExecuted for server-side caching, which swaps the HttpContext with a StringWriter-backed response to capture and store rendered HTML in the Cache object keyed by controller, action, and parameters. Browsing daily.dev surfaces patterns like this when wiring up caching in older ASP.NET MVC codebases."}},{"@type":"Question","name":"How can I vary cached output by request parameters in a custom ASP.NET MVC output cache implementation?","acceptedAnswer":{"@type":"Answer","text":"Generate a cache key that combines the controller's full type name, the action name from route data, and each route parameter matching a VaryByParam list (or all parameters when VaryByParam is set to \"*\"). This produces distinct cache entries per parameter combination, mirroring the VaryByParam behavior of the classic ASP.NET OutputCache directive. Developers comparing caching strategies often track implementation details like this through daily.dev."}}]}
```

