Implementing Filters in MVC

Authorization Filters:
This filter is used to check whether request coming to use action or controller is authorized to use that action or controller or not.
Usually it is used to authorize users who requests to controller or actions.
There is inbuilt filter [Authorize] which can be used to authorize users.
IAuthorizationFilter interface has OnAuthorization method which can be overridden to create custom authorization filter.
It also have OnUnauthorizedRequest method which can be used to redirect unauthorized requests.
Creating Custom Authorization Filter :

using System.Web.Mvc;
namespace myProjects.Filters
{
public class CustAuthFilter : AuthorizeAttribute
  {
   public override void OnAuthorization(AuthorizationContext filterContext)
     {            
            filterContext.Controller.ViewBag.AutherizationMessage = "On authorization.";
     }        
  }
}

Now Class CustAuthFilter can be used as attribute as shown bellow.

[CustAuthFilter]
Public ActionResult MyView()
{
	// logic
	return view();
}

Action Filters:
Action filter helps you to perform operations before or after action is executed.
IActionFilter interface has two methods which can be used to perform operations.
OnActionExecuting : It will be called before action is executed.
OnActionExecuted : It will be called after action is executed.

Creating Custom Action Filters :

using System.Web.Mvc;
namespace myProjects.Filters
{
     public class CustomActionFilter : ActionFilterAttribute, IActionFilter
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.Controller.ViewBag.CustomActionMessage1 = "On Action Executing.";
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            filterContext.Controller.ViewBag.CustomActionMessage2 = "On Action Executed.";
        }
    }
}

Now CustomActionFilter class can be used as attribute.

[CustomActionFilter]
Public ActionResult MyView()
{
return view();
}

Result Filters:
Action filter helps you to perform operations before or after content is rendered in view.
IResultFilter interface has two methods which can be used to perform operations.
OnResultExecuting : It will be called before content is rendered in view.
OnResultExecuted : It will be called after content is rendered in view.

Creating Custom Result Filters :

using System.Web.Mvc;
namespace myProjects.Filters
{
    public class CustomResultFilter : FilterAttribute, IResultFilter
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.Controller.ViewBag.Message = "On Result Executing";
        }
    }
   public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
            filterContext.Controller.ViewBag.Message2 = "On result Executed.";
     }        
  }
}

Now CustomResultFilter class can be used as attribute.

[CustomResultFilter]
Public ActionResult MyView()
{
return view();
}

Exception Filters:
It is used to capture ant exception raised by controller or action methods.
It has HandleError attribute which can be used to catch exceptions.
IExceptionFilter interface has OnException method which can be overridden to create custom exception filters.

Creating Custom exception filters:

using System.Web.Mvc;

namespace DisplayModes.Filters
{
    public class CustomExceptionFilter : FilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            filterContext.Controller.ViewBag.ExceptionMessage = "called on exception.";
        }
    }
}

This can be used on actions as well as controller

[CustomExceptionFilter]
public ActionFilter MyView()
{
return View();
}

admin