public override void OnActionExecuting(ActionExecutingContext filterContext) { _key = GetViewDataKey(filterContext); var allKeys = filterContext.Controller.TempData.Keys.ToArray(); //Access all tempdata to figure out which ones can be cleaned out (otherwise they just keep building up, one per action) foreach (var currKey in allKeys.Where(_ => _ != _key && _.StartsWith(ViewDataKeyPrefix))) { //pull it out of temp data var item = filterContext.Controller.TempData[currKey] as PreventDoubleActionData; if (item != null && !IsExpired(item)) { //put it back in, we might need it! filterContext.Controller.TempData[currKey] = item; } else { filterContext.Controller.TempData.Remove(currKey); } } _currentActionData = filterContext.Controller.TempData[_key] as PreventDoubleActionData; if (_currentActionData != null && !IsExpired(_currentActionData)) { //not expired, reuse it filterContext.Result = _currentActionData.Result; //put it back in, we might need it again! filterContext.Controller.TempData[_key] = _currentActionData; } else { _currentActionData = new PreventDoubleActionData { DateAdded = DateTimeOffset.Now, PreventionExpiryInSeconds = _preventionExpiryInSeconds, Result = null }; filterContext.Controller.TempData[_key] = _currentActionData; } }
private static bool IsExpired(PreventDoubleActionData item) { return(item == null || DateTimeOffset.Now.Subtract(new TimeSpan(0, 0, 0, item.PreventionExpiryInSeconds)) >= item.DateAdded); }