Creating an Ajax.ActionLink that avoids all caching issues
- by Richard Ev
I am using an Ajax.ActionLink to display a partial view that shows a settings dialog (the modality of which is arranged using jQuery UI dialog).
The issue I am running into is around browser caching. It is important that the user is never shown a cached settings dialog. In an attempt to achieve this I have written the following extension method that has the same method signature as the ActionLink method overload that I am using.
/// <summary>
/// Defines an AJAX ActionLink that effectively bypasses browser caching issues
/// by adding an additional route value that contains a unique (actually DateTime.Now.Ticks) value.
/// </summary>
public static MvcHtmlString NonCachingActionLink(this AjaxHelper helper, string linkText, string actionName, string controllerName, System.Web.Routing.RouteValueDictionary routeValues, AjaxOptions ajaxOptions)
{
routeValues.Add("rnd", DateTime.Now.Ticks);
return helper.ActionLink(linkText, actionName, controllerName, routeValues, ajaxOptions);
}
This works well between browser sessions (as the rnd route value gets re-calculated on page load), but not if the user is on the page, makes settings changes, saves them (which is done with another ajax call) and then re-displays the settings dialog.
My next step is to look into creating my own ActionLink equivalent that re-calculates a random query string component as part of the onclick JavaScript event handler.
Thoughts please.