Filters is basically used for authentication, authorization, validate or errors log before and after a controller action executes.
Different Types of Filters Are Available i.e.
1. Actions Filter
3. Result filters
4. Exception filters
Action Filters - Action filters are used to implement logic that gets executed before and after a controller action executes.
The Action filters have following -
1. Output Cache- Used for caches the output of a controller action for a specified amount of time.
2. Handle Error- Used for handles raised errors when a controller action executes.
3. Authorize – Used to enables you to restrict access to a particular user role and its permission.
Example for Output Cache –
OutputCache(Duration = 10)]
public IEnumerable<sp_Sel_Customer_Result> GetCustomers(string itemFilter)
string UserNo = null;
if (Request.Headers.Contains("User_No") != "")
UserNo = Request.Headers.GetValues("User_No").FirstOrDefault();
Authorization Filters- This filter is used to implements authentication and authorization for your controller actions. An example for Authorization filters in an ASP.NET MVC –
Result Filters- This filters contain logic that is executed before and after a view result executed.
Exception Filters- The exception filters is used to handle errors, log errors raised by your controller actions and it is executed last.
If you have multiple filters implanted, what is the order in which these filters get executed?
The filter order would be like -
1. Authorization filters
2. Action filters
3. Response filters
4. Exception filters
Authorization filters executed first and Exception filters in last.
What filters are executed in the end?
Exception Filters executed last. Exception filters is used to handle errors, log errors raised by your controller.
What filters are executed in the first?
Authorization Filters executed first. Authorization filters is used to implements authentication and authorization for your controller actions.
Is it possible to create a custom filter in MVC?
Yes! It is possible. You can create as per your app requirements.
What Type of filter does OutputCacheAttribute class represents?