Close Menu

    Subscribe to Updates

    Get the latest creative news from FooBar about art, design and business.

    What's Hot

    Inspiring Journey Career and Life Beyond Fame

    July 28, 2025

    Charles Ezekiel Mozes: A Detailed Overview

    July 28, 2025

    Saizeriya Singapore Menu Prices Updated 2023

    July 27, 2025
    Facebook X (Twitter) Instagram
    Tuesday, July 29
    Reflect Now
    Facebook X (Twitter) Instagram YouTube
    • Home
    • Travel
      • Hotels
      • Restaurants
    • Beauty
      • Fashion
      • Lifestyle
    • Casino
    • Real Estate
    Latest From Tech Buy Now
    Reflect Now
    Home » ASP.NET MVC Filter | Overview and Types of ASP.NET MVC Filters
    Real Estate

    ASP.NET MVC Filter | Overview and Types of ASP.NET MVC Filters

    AdminBy AdminJuly 27, 2025Updated:July 28, 2025No Comments6 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    ASP.NET MVC Filter | Overview and Types of ASP.NET MVC Filters
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link

    Updated March 15, 2023

    ASP.NET MVC Filter | Overview and Types of ASP.NET MVC Filters

     

     

    Introduction to ASP.NET MVC Filter

    ASP.NET MVC Filter is a custom class that executes before or after some action or controller method. It is routed to a suitable controller or action method based on user requests. ASP.NET MVC Filter makes available with appropriate requests given by user where we need to execute logic after or before action method performs. These filters are applied to the controller or action method in a programmatic method.

    Overviews of ASP.NET MVC Filters

    ASP.NET MVC Filters are mainly used to execute the custom logic after or before the execution of some action method, where we can write any custom logic to execute for particular execution.

    As we discussed earlier, the user will make a request, and depending on that, it executes before/ after some action or controller method. Once the client makes the request, it comes to the router’s engine and navigates that request to the controller. The controller chooses the appropriate action method to execute. The Controller action method handles the request and response back to the client. Let’s see the flow of the process below,

    ASP NET MVC Filters1ASP NET MVC Filters1

    For executing some logic before or after the action method is executed, let’s follow the below flow process,

    ASP NET MVC Filters2ASP NET MVC Filters2

    ASP.NET MVC Filters are the attributes that enable some logic to be executed either before or after an action method is called upon.

    Types of ASP.NET MVC Filters

    The Filters are applied through the programmatic or declarative method. The declarative method means applying the filter properties to the controller class or action method. The programmatic method includes interfaces applying or implementing a required interface. In ASP.NET MVC Filters, there are various types of Filters as follows,

    • Authentication Filter
    • Authorization Filter
    • Action Filter
    • Result Filter
    • Exception Filter

    ASP NET MVC Filters3ASP NET MVC Filters3

    Let’s see the overview of each filter

    1. Authentication Filter

    The initial filter is executed before the other filters/ action method is executed. The Authentication Filter checks whether the request is coming from a valid user. It implements the interface IAuthenticationFilter, this interface used to build for custom Authentication Filter. Let’s see the definition of the IAuthenticationFilter interface,

    Authentication Filter Authentication Filter

    2. Authorization Filter

    It will execute once after the execution of the authentication filter. The authorization filter is used to verify whether the user has all rights to access a particular page or resource. It implements the interface IAuthorizationFilter, the examples of built-in Authorization Filter are AuthorizeAttribute and RequestHttpsAttribute. Let’s see the definition of this filter,

    Authorization Filter Authorization Filter

    3. Action Filter

    It is executed before the action method starts or after an action’s execution. It implements the IActionFilter interface and has two methods: OnActionExecuting and OnActionExecuted. We can use custom logic before the action method starts, then we need to implement the OnActionExecuting method, and we need to create the custom logic; after the action method, we need to implement the OnActionExecuted method. Let’s see the definition of an Action Filter as follows,

    ASP.NET MVC Filter - Action Filter ASP.NET MVC Filter - Action Filter

    4. Result Filters

    They are executed after or before generating the result for the action method. There are various Action result type they are FileResult, ViewResult, JsonResult, RedirectResult, PartialViewResult, ContentResult, RedirectToRouteResult and EmptyResult. Those result types are derived from the ActionResult abstract class. The Result Filters are called only after the Action Filters. An example of a Result Filter is the in-built OutputCacheAttribute. It implements the IResultFilter interface. Let’s see the definition of the IResultFilter interface.

    ASP.NET MVC Filter - Result Filters ASP.NET MVC Filter - Result Filters

    The IResultFilter interface contains two methods, OnResultExecuting and OnResultExecuted. To execute the custom logic before generating the result, we must implement the OnResultExecuting method. For writing custom logic after generating the result, we need to implement the OnResultExecuted method. So we need to implement the IResultFilter interface for creating Custom Result Filter. There will be some set of order lists to execute the filters like in the before execution of action method authorization filter will execute likewise after execution method the exception will execute.

    5. Exception Filters

    They are executed when the unhandled exception happens during the execution of an action or the filters. An example of Exception Filters is the in-built HandleErrorAttribute. The interface IExceptionFilter is used to build a custom Exception Filter which offers an OnException method executed when an unhandled exception occurs at the time of execution of action or filters. Let’s see the definition of IExceptioFilter,

    ASP.NET MVC Filter - Exception FiltersASP.NET MVC Filter - Exception Filters

    Some predefined filters are already built by MVC Framework, which is ready to use; they are,

    • Authorize
    • ValidataInput
    • HandleError
    • RequireHttps
    • OutputCache

    Register Filters

    Register filters are applied on three levels; they are

    1. Global Level Filters
    2. Controller Level Filters
    3. Action Method Filters

    1. Global Level Filters: We can apply filters at the global level in the initial level Application_Start event of Global.asax.cs file with the help of FilterConfig.RegisterGlobalFilters() method. This level of global filters is applied to the controller and the action methods. We can apply HandleError globally in the MVC Application. Let’s see the sample code for Register global filter,

    // the class contains in Global.asax.cs file
    public class MvcFilterApplication : System.Web.HttpApplication
    {
    protected void Application_Start()
    {
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    }
    }
    // App_Start folder contains the FilterConfig.cs
    public class FilterConfig
    {
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
    filters.Add(new HandleErrorAttribute());
    }
    }

    2. Controller Level Filters: This filter is applied to the controller class; this level filter allows for entire action methods. Let’s see the sample code Action Filter on Controller; it is applied the methods of HomeController,

    [HandleError]
    public class HomeController : Controller
    {
    public ActionResult Index()
    {
    return View();
    }
    public ActionResult About()
    {
    return View();
    }
    public ActionResult Contact()
    {
    return View();
    }
    }

    3. Action Method Filters: For this filter, one or more filters can be applied to a particular action method. It is applied to the Index() action method; let’s see the sample code for Action Methods,

    public class HomeController : Controller
    {
    [HandleError]
    public ActionResult Index()
    {
    return View();
    }
    public ActionResult About()
    {
    return View();
    }
    public ActionResult Contact()
    {
    return View();
    }
    }

    Conclusion

    MVC Filter is a custom class used to execute before or after some action or controller method executes. In this article, we have seen various types of filters with their definitions and in ASP.NET MVC, registering filters at Global Level is easy. Hope the article helps you to understand.

    Recommended Articles

    We hope that this EDUCBA information on “ASP.NET MVC Filter” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

    1. ASP.NET Core Session
    2. ASP.NET Core JWT
    3. ASP.NET UpdatePanel
    4. ASP.NET ViewState

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Admin
    • Website

    Related Posts

    Key Challenges in B2B Portal Development and How to Overcome Them

    July 27, 2025

    Choosing the Right AC Unit for Your Home Size and Budget

    July 27, 2025

    Sloped Roof Design Enhances The Tranquility Of The Banyan Clubhouse

    July 27, 2025
    Leave A Reply Cancel Reply

    Editors Picks

    Fortnite Show is Coming to Both PlayStation and Xbox Consoles

    January 12, 2021

    Resident Evil Features 9 Feet Tall Lady

    January 12, 2021

    Call of Duty Ratings Fall to 4.5 Stars

    January 12, 2021

    New Update 14 of Call of Duty Launched

    January 5, 2021
    Top Reviews
    9.1

    Cyberpunk 2077 Players Should Avoid Mods Due to Vulnerabilities

    By Admin
    8.9

    Oblivion DLC Takes You to Leyawiin and Arena’s Gideon

    By Admin
    8.9

    Leaked Fortnite Skins and Cosmetic Items from v9.50 Update

    By Admin
    Advertisement
    Demo
    Demo
    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo
    Don't Miss
    Entertainment

    Inspiring Journey Career and Life Beyond Fame

    By AdminJuly 28, 2025

    Ashton Meem is best known as the former wife of NFL quarterback Russell Wilson but…

    Charles Ezekiel Mozes: A Detailed Overview

    July 28, 2025

    Saizeriya Singapore Menu Prices Updated 2023

    July 27, 2025

    Cryptocurrency क्या है? इसके प्रकार और यह कैसे काम करती है?

    July 27, 2025

    Subscribe to Updates

    Get the latest creative news from SmartMag about art & design.

    About Us
    About Us

    Your source for the lifestyle news. This demo is crafted specifically to exhibit the use of the theme as a lifestyle site. Visit our main page for more demos.

    We're accepting new partnerships right now.

    Email Us: info@example.com
    Contact: +1-320-0123-451

    Our Picks

    Fortnite 21: Mecha Team Leader Returns to the Store

    January 13, 2021

    Review: Top 10 Best Police Car Chasing Games

    January 12, 2021

    Why Daredevil in SpiderMan is More Exciting than Tobey

    January 12, 2021
    New Comments
    • A WordPress Commenter on Hello world!
    About Us
    About Us

    Your source for the lifestyle news. This demo is crafted specifically to exhibit the use of the theme as a lifestyle site. Visit our main page for more demos.

    We're accepting new partnerships right now.

    Email Us: info@example.com
    Contact: +1-320-0123-451

    Facebook X (Twitter) Pinterest YouTube WhatsApp
    Our Picks

    Inspiring Journey Career and Life Beyond Fame

    July 28, 2025

    Charles Ezekiel Mozes: A Detailed Overview

    July 28, 2025

    Saizeriya Singapore Menu Prices Updated 2023

    July 27, 2025
    Most Popular

    Inspiring Journey Career and Life Beyond Fame

    July 28, 2025

    Tips And Tricks For Summer To Give Your Home A Festive Colorful Look

    January 5, 2020

    Why Drinking Tea First Thing In The Morning Is Not Advisable For A Healthy Lifestyle

    January 6, 2020
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.