End Your If

Helping you solve those tough coding problems!

C# OutputCache with subdomains C# OutputCache with subdomains

Published on May 4, 2018 by Jamie Munro

The other day I was working on a project that was leveraging OutputCache. This little attribute is a fantastic way to implement caching in an MVC project. It was being used as follows but it was not working when we had multiple subdomains sharing the same controllers and actions:

public ActionResult Index()
return View();

To make things more consistent across all methods that require caching, we defined a CacheProfile. This CacheProfile can then be setup in the Web.config as follows:

<!-- https://msdn.microsoft.com/en-us/library/ms164606(v=vs.100).aspx -->
<add name="CacheProfile" duration="31557600" enabled="true" varyByParam="*" />

By default we are caching for one year. We also want our caching to not simply cache the index page for all requests. Instead we included a varyByParam option that will tell the caching mechanism to cache different pages by the parameters passed into that page.

For us this started working great. However, things randomly started going a miss. Our project was sharing the same controllers and views for multiple subdomains. Each subdomain had it's on styling, but the controllers and views contained the same content.

After much headache and research, we discovered that when you share code between different subdomains or even different domains, it's important to include this configuration option:

This configuration option goes in the Web.config in the CacheProfile key next to the varyByParam.

Thanks to Zygimantas from When using OutputCache with MVC, sub.domain.com gives me www.domain.com cached page.

This saved the entire team over a day of research and head banging! Upvote the answer as it is well deserved!

Tags: Optimization | ASP.NET

My Books
blog comments powered by Disqus