How to bust/clear DonutCache in asp.net MVC

Let us say you have an asp.net MVC controller/action which is donutcached like below. And you want to bust the cache for some reason. An example would be. You cache a partial view for 24 hours, but give the user a refresh button to allow him to manually refresh it if he wishes to.

NOTE: you cannot use web.config based cacheprofiles for MVC. It just doesn’t work, so DonutCache is being used for that.

[DonutOutputCache(CacheProfile = "CachedAction")]
public ActionResult CachedAction(string id)
{
return View();
}

Here is the web.config

	<caching>
	  <outputCache enableOutputCache="true" />
	  <outputCacheSettings>
		<outputCacheProfiles>
			<add name="CachedAction" duration="14100" varyByParam="*" location="Any" />
		</outputCacheProfiles>
	  </outputCacheSettings>
	</caching>

Normally, in the javascript if you just call $.ajax() and request that action, it will just come back with the cached copy. The trick here is to first bust your server cache. So, you can create another action like so.

public void BustCache(string id)
{	
var Ocm = new OutputCacheManager();
RouteValueDictionary rv = new RouteValueDictionary();

if (!string.IsNullOrEmpty(id))
rv.Add("id", id);
Ocm.RemoveItems("controller", "cachedaction", rv);
}

Finally you would simply make two ajax calls. First, you bust your cache and then you call your regular MVC action to get your content.

$.get('@Url.Action("bustcache", "controller"');
$.get('@Url.Action("cachedaction", "controller")');

IIS Rewrite Rules (force www to non-www and http to https)

  • Redirect www site to non-www. eg. I use the rule below for my own website. If user browses to https://www.indexedmind.com, redirect them to https://indexedmind.com
    <system.webServer>
    <rewrite>
    <rules>
    <rule name=”Redirect WWW to non-WWW” stopProcessing=”true”>
    <match url=”(.*)” />
    <conditions>
    <add input=”{HTTP_HOST}” pattern=”^indexedmind\.com$” negate=”true” />
    </conditions>
    <action type=”Redirect” url=”https://indexedmind.com/{R:1}” />
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
    
  • Redirect http request to https. eg. if user browses to http://indexedmind.com, force them to go to https://indexedmind.com
    <rule name=”Redirect to HTTPS” stopProcessing=”true”>
    <match url=”(.*)”/>
    <conditions>
    <add input=”{HTTPS}” pattern=”^OFF$”/>
    </conditions>
    <action type=”Redirect” url=”https://{HTTP_HOST}{REQUEST_URI}” redirectType=”SeeOther”/>
    </rule>