Skip to Content
Beginning ASP.NET MVC 1.0
book

Beginning ASP.NET MVC 1.0

by Simone Chiaretta, Keyvan Nayyeri
August 2009
Beginner to intermediate content levelBeginner to intermediate
575 pages
13h 16m
English
Wrox
Content preview from Beginning ASP.NET MVC 1.0

Chapter 11. Action Filters

In the previous chapter, you learned how view components and HTML helpers can encapsulate the rendering of the UI, but they cannot be used to encapsulate the process logic. This can be achieved using action filters.

In this chapter, you learn:

  • What you can use action filters for

  • What kinds of filters are available

  • Which core filters are available inside the framework

  • How to write custom filters

What Is an Action Filter?

An action filter is a custom attribute that you can decorate action methods or controllers with. The framework will then execute the logic inside the action filter before or after the execution of the action method.

You probably already have seen a filter being used. It's in the HomeController that is added by the project template.

[HandleError]
public class HomeController : Controller

By being applied directly to the class definition, the filter is automatically applied to all the actions of the controller. This in particular instructs the ASP.NET MVC framework to handle the errors that might happen inside the actions, using the logic that is defined inside the filter.

If you wanted to handle the errors only for a specific action, you would annotate the method directly:

[HandleError]
public ActionResult Index()

In a typical ASP.NET MVC application, an action is responsible for handling user-initiated requests. The user clicks a button or submits a form, the request is sent and routed through the routing logic to a specific controller, and finally a ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning ASP.NET MVC 4

Beginning ASP.NET MVC 4

José Rolando Guay Paz
Programming ASP.NET MVC 4

Programming ASP.NET MVC 4

Jess Chadwick, Todd Snyder, Hrusikesh Panda
Professional ASP.NET MVC 2

Professional ASP.NET MVC 2

Jon Galloway, Phil Haack, Scott Hanselman, Scott Guthrie, Rob Conery

Publisher Resources

ISBN: 9780470433997Purchase book