Today, we are going to learn about the filters in ASP.NET MVC. We will be dividing this article as follows:
- What are filters
- Types of filters
- Action Filters
What are the filters
Sometimes we would like to perform certain action before or after a particular operation, or some times we need a pre or post action behaviors from action, for achieving this functionality ASP.NET MVC provides a feature called Filters.
Types of filters
ASP.NET MVC supports following types of filters:
- Action Filters
- Authorization Filters
- Result Filters
- Exception Filters
- Action Filters:
Authorization Filters
It is used to implement authorization and authentication for action filters
Result Filters
Result filters contains logic that gets executed before or after a view result gets executed. E.g. if you want to change view before its get render to browser.
Exception Filters
Exception filters are used to handle error, caused by either controller action or controller action results, we can also use it for logging the exceptions.
Action Filter in ASP.NET MVC
Action Filters can be applied to either controller action or controller itself, with the help of action filter we can change the way, the action or controller gets executed.
MVC provides following action filters
- Output Cache

This filter caches the output of action for certain duration. E.g. below code snippet, we are decorating login action with output cache keyword, that will cache the output of login action for 20 seconds( we have given 20 seconds duration).
- Handle Error
It handles the error caused by action or controller, if any exception occurs it redirects the action to custom error page. e.g: here in the below code snippet handle error attribute is decorated to login action method, it will redirect to a view called "error.cshtml" when any exception will occur by this action method.

- Authorize
It is used for filtering the authorized user to access the resource. E.g. in below code snippet we have decorated an action method with Authorize attribute.

- If we will try to access this action then it will give following error:

So these are the action filters, hopefully I was able to satisfy you, more will come with the next article on custom filters.
Thanks!
Happy Coding.
Happy Coding.
Read more articles on ASP.NET:


Sandip G PatilPosted Dec 17, 2018, 5:13 PM EST
Nice article...but not elaborated properly
Siva PrasadPosted Jun 5, 2018, 9:20 AM EDT
Its not elaborated properly, but its like a beginners one.
Sudheer NaiduPosted Mar 1, 2018, 7:09 PM EST
You are missing Authentication Filter in "types of filters"
mahesh pandianPosted Sep 15, 2017, 3:39 AM EDT
Good article. Very useful for beginners
Manav PandyaPosted Jun 6, 2017, 9:23 PM EDT
But u should be more specific
Manav PandyaPosted Jun 6, 2017, 9:23 PM EDT
Nice one ..................
Bhuvanesh MohankumarPosted May 3, 2016, 4:25 AM EDT
Nice...
Amit DavePosted May 3, 2016, 2:55 AM EDT
Nice article!
Hari ShankerPosted May 2, 2016, 8:16 PM EDT
nice
Thiruppathi RPosted May 2, 2016, 7:45 PM EDT
Good article
Bhavik PatelPosted May 2, 2016, 6:18 PM EDT
nice
Munesh SharmaPosted May 2, 2016, 10:43 AM EDT
good one
sreenivasa kPosted May 2, 2016, 10:13 AM EDT
good
Atul SrivastavaPosted May 2, 2016, 10:01 AM EDT
Thanks Kuppurasu
Kuppurasu NagarajTOP 100Posted May 2, 2016, 9:48 AM EDT
Nice Sharing..
Debasis SahaTOP 100Posted May 2, 2016, 9:47 AM EDT
Good share..