Close Menu

    Subscribe to Updates

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

    What's Hot

    Best Spice for Chicken: A Flavorful Guide to Seasoning Your Chicken 2024

    July 26, 2025

    Henry Aronofsky Rising Star Background and Future Prospects

    July 26, 2025

    Glimpsing The Future Of Online Game Worlds

    July 26, 2025
    Facebook X (Twitter) Instagram
    NetCelebz Wednesday, July 30
    • Demos
    • Buy Now
    Facebook X (Twitter) Instagram
    Subscribe
    • Home
    • Features
      • Example Post
      • Typography
      • Contact
      • View All On Demos
    • Home
    • Travel
      • Hotels
      • Restaurants
    • Beauty
      • Fashion
      • Lifestyle
    • Typography
    • Casino
    • Real Estate
    • Buy Now
    NetCelebz
    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

    dfasdt4By dfasdt4July 25, 2025Updated:July 26, 2025No Comments6 Mins Read0 Views
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Copy Link Email
    Follow Us
    Google News Flipboard
    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

    Follow on Google News Follow on Flipboard
    Share. Facebook Twitter Pinterest LinkedIn Telegram Email Copy Link
    dfasdt4
    • Website

    Related Posts

    Boost Your Business with Expert Google Ads Management Services

    July 26, 2025

    776625 Vanity Light | Illuminate Your Space

    July 26, 2025

    Landscaping Was The Best Money We Spent This Year

    July 26, 2025
    Leave A Reply Cancel Reply

    Demo
    Top Posts

    Wall Mounted Desks Vs. Other Desk Types: A Comparative Analysis

    July 25, 20251 Views

    Vegetarian Frozen Meal Delivery: Highly Nutritional and Convenient

    July 24, 20251 Views

    How to Stay Safe While Exploring the World Alone

    July 24, 20251 Views

    Best Spice for Chicken: A Flavorful Guide to Seasoning Your Chicken 2024

    July 26, 20250 Views
    Don't Miss

    Best Spice for Chicken: A Flavorful Guide to Seasoning Your Chicken 2024

    July 26, 20256 Mins Read0 Views

    IntroductionWhen it comes to preparing chicken, choosing the right seasoning can make all the difference.…

    Henry Aronofsky Rising Star Background and Future Prospects

    July 26, 2025

    Glimpsing The Future Of Online Game Worlds

    July 26, 2025

    Boost Your Business with Expert Google Ads Management Services

    July 26, 2025
    Stay In Touch
    • Facebook
    • Twitter
    • Pinterest
    • Instagram
    • YouTube
    • Vimeo

    Subscribe to Updates

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

    Demo
    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

    Best Spice for Chicken: A Flavorful Guide to Seasoning Your Chicken 2024

    July 26, 2025

    Henry Aronofsky Rising Star Background and Future Prospects

    July 26, 2025

    Glimpsing The Future Of Online Game Worlds

    July 26, 2025
    Most Popular

    The Best Gifts for Women to Receive for Mother’s Day and Beyond

    January 4, 20200 Views

    New High Tech Number Plate to Detect Uninsured Drivers

    January 5, 20200 Views

    Teenage Girl Finds Mom’s Debit Card, Spends $64,000 on Mobile Games

    January 6, 20200 Views
    • Home
    • Buy Now
    © 2025 ThemeSphere. Designed by ThemeSphere.

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