Hello Friends, In this tutorial we are going to learn Filters in ASP.NET MVC. Sometimes we come to situations that we need to execute some block of code before or after the execution of Action Method. For such scenario, ASP.NET MVC provides us Filters.

After Completing this tutorial you will be able to understand:

  • What are Action Filters in ASP.NET MVC?
  • Different types of Filters.

What are Filters?

Filters in ASP.NET MVC is a way to implement cross-cutting logic (For Example Security and Logging). Sometimes we need to execute some logic before or after executing an action. For such scenario, ASP.NET MVC provides us Filters.

ASP.Net MVC provides us some built-in Filters:

1- Output Cache:  This action filter caches the output of a controller action for a specified amount of time.

2- Handle Error: This action filter handles errors raised when a controller action executes.

3- Authorize: This action filter enables you to restrict access to a particular user or role.

Different Types of Filters:

Action Filters:

Action Filters can be used in a situation when we would like to execute logic before or after the execution of Controller Action. Action Filters implement IActionFilter interface that have two methods called OnActionExecuting and OnActionExecuted. OnActionExecuting method runs before the Action method executes and OnActionExecuted runs after the action method executed.

Authorization Filters:

Authorization Filters can be used to implement Authentication and Authorization for Controller actions. Authorization Filters implements IAuthorization interface. Authorize is an example of Authorization Filters.

Result Filters:

It is similar to Action Filters which is used to run a logic before or after the execution of View Result. This implements IResultFilter interface that have two method OnResultExecuting and OnResultExcuted. OutputCache attribute is an example of Result Filters.

Exception Filters:

Exception Filters are used to handle errors raised by Controller Action or Action Results. We can also log errors using Exception Filters.

Filters

Filter Order:

We can apply one or more than one Filters on a Controller, class or Action method. These Filters run in following orders.

  • Authorization Filters
  • Action Filters
  • Result Filters
  • Exception Filters

Register Filters:

You can specify Filters at different level.

1- Global Level:

You can apply filters at global level in the Application_Start event of Global.asax.cs file by using default FilterConfig.RegisterGlobalFilters() mehtod. Suppose you want to apply Exception Filter on All Controller and action method. You just need to modify your Global.asax.cs file as below.

public class FilterDemo : System.Web.HttpApplication

{

    protected void Application_Start()

    {

        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    }

}

public class FilterConfig

{

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)

    {

        filters.Add(new HandleErrorAttribute());

    }

}

2- Controller Level:

You can also apply the filter to specific Controller. That filter will be applied to all action method of that controller. Here is an example of applying Filters at Controller level.

[HandleError]

public class HomeController : Controller

{

    public ActionResult Index()

    {

        return View();

    }




}

3- Action Level:

You can also apply Filter to specific Action method only. The filter will only be applied to that Action method.

public class HomeController : Controller

{

    [HandleError]

    public ActionResult Index()

    {

        return View();

    }




}

View More:

Conclusion:

Hope you loved this post about Filters in ASP.NET MVC. I will appriciate your Feedback, Comments and suggestions.

Thank You.

 

Leave a Comment