Custom Action Filters


InterServer Web Hosting and VPS
Sony Playstation PS4 Pro Custom Build NVMe SSD Drive - Enclosure case and Crucial NVMe SSD
PS4 Pro Custom External SSD NVMe Drive

How I built a PS4 Pro Custom External SSD NVMe Drive...

NUC8i7BEH with memory
Intel NUC8I7BEH Review and Custom Build

This is my review of the Intel NUC8i7BEH and a summary of my build...

Card image cap
Asus M5A88-M EVO Review

This is my review of the Asus M5A88-M EVO motherboard...

Corsair Vengeance Blue 8GB (2x4GB) DDR3 PC3-12800C9 1600MHz Dual Channel Kit
Corsair Vengeance Blue Review

This is my review of the Corsair Vengeance Blue 8GB (2x4GB) DDR3 PC3-12800C9 1600MHz Dual Channel Kit memory...

Robotic Arm with USB PC Interface Review
Robotic Arm with USB PC Interface Review

This is my review of the Robotic Arm with USB PC Interface...

ASP.NET MVC Action Filters

Here we look at creating custom Action Filters in ASP.NET MVC.

ActionFilterAttribute

When creating new custom action filters in ASP.NET MVC we need to create a class which inherits from ActionFilterAttribute. From this we can implement the following methods:


OnActionExecuting
OnActionExecuted
OnResultExecuting
OnResultExecuted


Here is a sample class which implements these methods:


using System.Web.Mvc;

namespace MVC_Practise.Controllers
{
    public class MyFirstCustomActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // do something

            base.OnActionExecuting(filterContext);
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            // do something

            base.OnActionExecuted(filterContext);
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            // do something

            base.OnResultExecuting(filterContext);
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            // do something

            base.OnResultExecuted(filterContext);
        }
    }
}


With our newly ceated custom action filter we can now apply it to individual action methods or an entire controller classes (in which case every action method will use the attribute):


[MyFirstCustomActionFilter]
public ActionResult About()
{
    return View();
}


Applying our custom action filter to an action method called About results in the following order of methods firing:


OnActionExecuting
About
OnActionExecuted
OnResultExecuting
OnResultExecuted


Sponsored Links
SolarSystem - AngularJS Demo
SolarSystem - Angular JS Demo

Check out my first Angular JS demo hosted in Azure, it helped win my first Angular contract. Put the sound on.