X

Global Action Filters in ASP.NET MVC3

Action Filters are available from ASP.NET MVC 2. Action Filters are the attributes used to apply pre processing  or post processing logic on the Action methods and Action Results declarative fashion (i.e.: before executing an action method or after executing action method , before returning an action result or after returning action result). For example…

Action Filters are available from ASP.NET MVC 2. Action Filters are the attributes used to apply pre processing  or post processing logic on the Action methods and Action Results declarative fashion (i.e.: before executing an action method or after executing action method , before returning an action result or after returning action result).

For example [HandleError] is attribute we can apply on Action method in side a controller. Using [HandleError] we can  specify how to handle an exception that is thrown by an action method. By default, when an action method with the [HandleError] attribute throws any exception, ASP.NET MVC displays the Error view (Error.cshtml/Error.vbhtml/Error.aspx) that is located in the ~/Views/Shared/ folder in Application.

Using [HandleError] we can apply the exception handling logic on a action method by following syntax:

[HandleError]
public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";
    return View();
}

Now while processing index action method if any error occurs [HandleError] attribute will handle it, for example if one more action method, we want apply the same exception handling mechanism, we can use same syntax.

For example we want apply same the same exception handling mechanism to all the action methods ,so instead of applying [HandleError] attribute on each action method , we can directly apply the attribute on controller using same syntax.

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewBag.Message = "Welcome to ASP.NET MVC!";
        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

Now the question is how to apply the same exception handling mechanism to all the action methods (or) all controllers  (at global level) in ASP.NET MVC application?

There is no direct way to do this in ASP.NET MVC 2, we can do this on our own in multiple ways, like we write custom controller base class add  the logic to that custom controller base class & let the all the controllers in application inherit from custom controller base class, etc.

ASP.NET MVC 3 come with a built in solution for this i.e: Global Action Filters , now we can register action filters at global level , that is we need to register global filters during application start-up, so that global filters executes on every request, to do this we should add the instance of action filter to GlobalFilterCollection.

GlobalFilters.Filters.Add(new HandleErrorAttribute());

If we look at the Global.asax in ASP.NET MVC 3 solution [HandleError] attribute is already added to GlobalFilterCollection collection.

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    
    RegisterGlobalFilters(GlobalFilters.Filters);
    
    RegisterRoutes(RouteTable.Routes);
}

As shown above we can add any built in action filters or custom action filters  globally from ASP.NET MVC 3 onwards. If want want to more understand about different types filters you can read my article on filters.

Categories: ASP.NET MVC
Shravan Kumar Kasagoni: