O'Reilly logo

Professional ASP.NET MVC 2 by Rob Conery, Scott Guthrie, Scott Hanselman, Phil Haack, Jon Galloway

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. Filters

Chapter 5 covered Controllers and Controller actions, which are responsible for coordinating interactions between the user, the Model, and the View. A given action method typically handles a specific user interaction with the View.

For example, when a user clicks a link to display all products on a page, that click might create a request to an action method of ProductsController named List, which reaches into the domain objects, grabs all the products, slaps them together with the View, and displays the result to the user.

Grabbing the product data and selecting the View is the primary responsibility of that action method. It's not responsible for cross-cutting concerns such as logging, output caching, authentication, and authorization. These are responsibilities that your action method shouldn't need to know about. In mathematical parlance, they are orthogonal to the purposes of your action method.

This is where filters come into play. Filters are a declarative attribute-based means of providing cross-cutting behavior to an action method.

FILTERS INCLUDED WITH ASP.NET MVC

Before you delve into how one goes about writing a filter, let's take a look at the filters that are included with ASP.NET MVC.

ASP.NET MVC includes the following four Action filters out of the box:

  • Authorize: This filter is used to restrict access to a Controller or Controller action.

  • HandleError: This filter is used to specify an action that will handle an exception that is thrown from inside an action ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required