ma.citi

A Coder's Blog

ActionFilterAttribute.

ActionFilterAttribute is an abstract class upon which action filters are based. It allows the creation of custom action filters.

There are 4 methods that a custom action filter that implements ActionFilterAttribute can override:

Example of custom action filter

I created a simple custom action filter. The purpose of this filter is to clear a portion or the entire content of the cache once a particular controller action is hit.

    public class ClearCache : ActionFilterAttribute
    {
        public bool ClearAll { get; set; }

        public string Key { get; set; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var cacheProvider = new SimpleCacheProvider();

            if (ClearAll)
            {
                cacheProvider.ClearAll();
            }

            if (!String.IsNullOrEmpty(Key))
            {
                cacheProvider.Clear(Key);
            }
        }
    }

I added 2 properties, ClearAll to clear the entire content of the cache and Key, to clear a specific key/value from the cache.

This is the simple implementation that I wrote for the cache provider:

    public interface ICacheProvider
    {
        bool Get<T>(string key, out T value);

        void Set<T>(string key, T value);

        void Set<T>(string key, T value, int duration);

        void Clear(string key);

        void ClearAll();
    }
	
	public class SimpleCacheProvider : ICacheProvider
    {
        private readonly int defaultCacheDurationInMinutes = 30;

        private Cache cache;

        public SimpleCacheProvider()
        {
            cache = HttpRuntime.Cache;
        }

        public void Clear(string key)
        {
            cache.Remove(key);
        }

        public void ClearAll()
        {
            List<string> keys = new List<string>();

            foreach (DictionaryEntry item in cache)
            {
                keys.Add(item.Key as string);
            }

            keys.ForEach(x => cache.Remove(x));
        }

        public bool Get<T>(string key, out T value)
        {
            try
            {
                if (cache[key] == null)
                {
                    value = default(T);
                    return false;
                }

                value = (T)cache[key];
            }
            catch
            {
                value = default(T);
                return false;
            }

            return true;
        }

        public void Set<T>(string key, T value)
        {
            Set<T>(key, value, defaultCacheDurationInMinutes);
        }

        public void Set<T>(string key, T value, int duration)
        {
            cache.Insert(key,value,null, DateTime.Now.AddMinutes(duration), TimeSpan.Zero);
        }
    }

Use the attribute on controller action

        [ClearCache(ClearAll = true)]
        public ActionResult TestActionX()
        {
            return View();
        }
		
		[ClearCache(Key = "test")]
        public ActionResult TestActionY()
        {
            return View();
        }