Click here to Skip to main content
15,892,768 members
Articles / Web Development / ASP.NET

ASP.NET MVC controller action with Interceptor pattern

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
2 Oct 2012CPOL8 min read 67.8K   924   24  
This article is to demonstrate interceptor pattern with MVC controller action, and so action can be intercepted in controller classes without using action filters.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MvcCallInterceptors.Controllers;
using System.Reflection;

namespace MvcCallInterceptors.Interceptors
{
    public class ActionInterceptor
    {
        Type _interceptorControllerType;
        ActionInterceptorAttribute _actionInterceptorProperties;
        internal MethodInfo InterceptorMethodInfo;

        public ActionInterceptor(Type interceptorControllerType, ActionInterceptorAttribute actionInterceptorProperties, MethodInfo interceptorMethodInfo)
        {
            if (interceptorControllerType == null || actionInterceptorProperties == null || interceptorMethodInfo == null)
            {
                throw new Exception("Failed to initialize required parameters for ActionInterceptor");
            }
            _interceptorControllerType = interceptorControllerType;
            _actionInterceptorProperties = actionInterceptorProperties;
            InterceptorMethodInfo = interceptorMethodInfo;
        }
        public Type InterceptorControllerType
        {
            get { return _interceptorControllerType; }
        }

        public ActionInterceptorAttribute ActionInterceptorProperties
        {
            get { return _actionInterceptorProperties; }
        }

        public override bool Equals(object obj)
        {
            var actionInterceptor = obj as ActionInterceptor;
            if (actionInterceptor == null )
                return false;
            return new ActionInterceptorComparer().Equals(this, actionInterceptor);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Like to dream on long drive and falling asleep on short drive..!

Comments and Discussions