Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

What is Filters in ASP.NET MVC 5

In this article, I will explain most frequently asked asp.net MVC interview questions: What is filters in MVC? ASP.Net MVC filters are used to inject extra logic at the different levels of MVC framework request processing.

  • Keep reading on Custom Paging in MVC using jQuery, ASP.NET MVC full Ajaxify and Bootstrap Grid with Crud Operation

What are different types of Filters in ASP.Net MVC

ASP.NET MVC Filter is a custom class where you can write custom logic to execute before or after an action method executes. Filters can be applied to an action method or controller in a declarative or programmatic way. Declarative means by applying a filter attribute to an action method or controller class and programmatic means by implementing a corresponding interface.

MVC provides different types of filters. The following table list filter types, built-in filters for the type and interface which must be implemented to create a custom filter class.

Filter Type Description Built-in Filter Interface
Authorization filters Performs authentication and authorizes before executing an action method. [Authorize], [RequireHttps] IAuthorizationFilter
Action filters Performs some operation before and after an action method executes. IActionFilter
Result filters Performs some operation before or after the execution of the view results. [OutputCache] IResultFilter
Exception filters Performs some operation if there is an unhandled exception thrown during the execution of the ASP.NET MVC pipeline. [HandleError] IExceptionFilter

For better understanding let’s take an example of a built-in Exception filter.

An Exception filter executes when there is an unhandled exception occurs in your application. HandleErrorAttribute ([HandlerError]) class is a built-in exception filter class in MVC framework. This built-in HandleErrorAttribute class renders Error.cshtml included in the Shared folder by default when an unhandled exception occurs.

[HandleError]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        //throw exception for demo
        throw new Exception("This is unhandled exception");
            
        return View();
    }

    public ActionResult About()
    {
        return View();
    }

    public ActionResult Contact()
    {
        return View();
    }        
}

Please make sure that CustomError mode is on in System.web section of web.config, in order for HandleErrorAttribute work properly.

Now, if you run the application. You would get the following error page because we throw an exception in the Index action method for the demo purpose.

So, the HandleError attribute will display a common error page for any unhandled exception that occurred in HomeController.

  • Filters can be applied globally in FilterConfig class, at controller level or action method level.
  • Custom filter class can be created by implementing the FilterAttribute class and corresponding interface.

Filter Order

MVC includes different types of filters and multiple filters can be applied to a single controller class or action method. So, filters run in the following order.

  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters

Conclusion

I hope you liked this article on filters in MVC 5. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

The post What is Filters in ASP.NET MVC 5 appeared first on DotNetTec.



This post first appeared on Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps API Developer, please read the originial post: here

Share the post

What is Filters in ASP.NET MVC 5

×

Subscribe to Asp Dot Net Tricks And Tips, Dot Net Coding Tips, Google Maps Api Developer

Get updates delivered right to your inbox!

Thank you for your subscription

×