ASP.NET MVC 3 Custom Action Filters

Version: 1.1.0


In ASP.NET MVC 3 Global, Dynamic Filters Hands-on Lab, you have been adding filters to MVC processing. In this Hands-on Lab, you will use custom filters to log your site activity. To achieve that, in the proposed exercise you will create a custom filter attribute that will work with any specified controller. That filter will catch controller's requests, and save them into a database table. Through a log view you will be able to see the list of visits that each filtered controller had.


This Hands-on Lab assumes you have basic knowledge of ASP.NET MVC. If you have not used ASP.NET MVC before, we recommend you to go over ASP.NET MVC Fundamentals Hand-on Lab.

ASP.NET MVC provides Action Filters for performing logic either before an action method is called or after its run. Action Filters are custom attributes that provide a declarative means to add pre-action and post-action behavior to controller action methods.

In this Hands-on Lab you will create a custom action filter attribute into MVC Music Store solution to catch controller’s requests and log site actvity into a database table. You will be able to add your logging filter by injection to any controller or action. Finally, you will see the log view that shows a visit list.


In this Hands-On Lab, you will learn how to:

  • Create a custom action filter attribute to extend filtering capabilities
  • Apply a custom filter attribute by injection to a specific level

System Requirements

You must have the following items to complete this lab:

  • ASP.NET and ASP.NET MVC 3 Release Candidate
  • Visual Studio 2010 Express
  • SQL Server Database (Express edition or above)

    You can install the previous system requirements by using the Web Platform Installer 3.0:


Installing Code Snippets

For convenience, much of the code you will be managing along this lab is available as Visual Studio code snippets. To install the code snippets run .\Source\Assets\CodeSnippets.vsi file.


This Hands-On Lab is comprised by the following exercises:

  1. Exercise 1: Logging actions

Estimated time to complete this lab: 20 minutes.

Note: Each exercise is accompanied by an End folder containing the resulting solution you should obtain after completing the exercises. You can use this solution as a guide if you need additional help working through the exercises.

Next Step

Exercise 1: Logging Actions