15 49.0138 8.38624 1 0 4000 1 https://codeisall.com 300
Filters in MVC codeisall.com

Filters in MVC

Introduction

In the previous article, you have learned about about what is Areas, the advantages of Areas, and how to use it in an MVC application.

In this article, you will learn about what is Filters, types of filters, advantages of filters and how to use it in an MVC application.

This article will be helpful to beginners and students who are learning .

What is Filters?

Filter is a piece of code or a method that you want to execute before or after any request routed to the appropriate controller and action method.

MVC Filter is a custom class where you can write custom logic to execute before or after an action executes. 

Filters can be declared on the method or a controller by filter attribute or it can be applied programmatically by implementing a corresponding interface.

That type of implementation is called cross-cutting concerns. So, in simple words, this adds extra logic to be implemented into the request being processed. Some of the examples of cross-cutting concerns are Authorization & Output Caching.

Types of Filters in ASP.NET MVC:

There are five types of filters in MVC

Authentication Filters :

those are used to confirm that you are valid or invalid user while you request any action, if you are valid user then only you can access that method.

Authentication Filters implement IAuthenticationFilter  interface.

Authorization Filters :

Authorization filters are used to check the user access while they request any action.  Authorize attribute and Require Https attribute are examples of Authorization Filters . The Authorize filter is an example of an Authorization filter.

Authorization filters implements IAuthorizationFilterinterface .

Action Filters:

Those are used to run the piece of code before or after calling any action, and after the action has executed. Action filter is an attribute that you can apply on the action or on the entire controller.

It’s implement the IActionFilter interface,  it contains two methods OnActionExecuting and OnActionExecuted. There filters contains the logic that can be executed after or before calling an action. Action filters can be used to modify the view data the returns by the action.

Result Filters:

Result filters contains a code that can be executed before and after a view result is executed. If you want to modify a view result right before the view is rendered to the browser.

Result Filters implement the IResultFilter interface which is like the IActionFilter has OnResultExecuting and OnResultExecuted. An example of Result Filters is OutputCacheAttribute class. 

ExceptionFilters :

Exeception filters filters can be used as an exception filter to handle errors raised by your controller action results.

Exception Filters implement the IExceptionFilter interface and they execute if there are any unhanded exceptions thrown during the execution. An example of Exception Filters is HandleErrorAttribute class.

Example :

Authentication Filters :

Create a class and replace the below code to create the custom authentication. ResloveActionFilterAttribute and IAuthenticationFilter form the “using System.Web.MVC.Filters;” namespace.

Now to use this custom authentication, on action write the class name as an attribute as shown in below code. Add the reference of the class that you have created.

Authorization Filters :

This filters is used to ensure that the requested action can be invoked by the approved users only. The common example of authorization filter is role based access to any action, suppose there are two role admin and user , where user can only view data and admin can manipulate the data.

Use the created filter in the Home.cs controller.

Action Filters:

There are 3 types of Action filters:

  • Output Cache: This action filter caches the output of a controller action.
  • Handle Error: This action filter handles errors raised when a controller action executes.
  • Authorize: This action filter enables you to restrict access to a particular user or role.

Output Cache :

It save the output of the method for 10 seconds in a cache.

Handle Error:

If any error occurs while executing the action, it returns the error page and show error to the user.

Authorize:

It allows only authorize user to access the method.

Result Filters :

Result filters are executed before or after generating the result for an action.The Action Result type can be ViewResult, PartialViewResult, RedirectToRouteResult, which derives from the ActionResult class.

Exception Filters :

Exception filters are run only if any exception occurs when run any action. below interface is used for that.

Below method invokes on exception.

I hope this article will help you to understand about filters and how useful in web applications, how can be used in MVC applications and also what is the advantages of MVC filters. We will be happy to help you if you have any issues or queries. Comment your suggestions and issues and we will try to resolve it.

Share:
Category:MVC
PREVIOUS POST
Save Data with AJAX in MVC