ASP.NET MVC 3 Global and Dynamic Action Filters
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 previous versions of MVC, action filters were applied by making a filter attribute and apply it to a controller or an action method. MVC 3 provides the ability to define filters outside that scope, giving the possibility to apply filters to all actions in an MVC application. In ASP.NET MVC 2 Membership, Authorization and Store Checkout you have been working with the security of your web application. In this Hands-on Lab you will use Global Filters and will apply them to site logging. In order to achieve that, in the first exercise you will learn how to use the filters and how to customize an Attribute filter for global security. Then, in the second exercise you will learn how to create a Global Dynamic Filter by using MVC Filter Providers. That consists in a filter in which execution depends on the context.
This Hands-on Lab assumes you have basic knowledge of ASP.NET MVC and ASP.NET MVC Custom Action Filter. If you have not used ASP.NET MVC before, we recommend you to go over ASP.NET MVC Fundamentals Hand-on Lab.
If you followed ASP.NET MVC Custom Action Filter Hands-on Lab before, you have applied an action filter as an attribute to a controller or action method. In this lab, you will learn how to apply a filter to all controllers in the application (global action filter) and how to apply them dynamically.
Similarly than in ASP.NET MVC Custom Action Filter, in this Hand-on Lab, you will be working with actions logging, writing events in a database table and showing the result in a dedicated controller and view.
In the first exercise you will apply global filters using code residing in the Global.asax.[cs|vb] source file. In the second exercise you will learn how dynamically define which actions will get logged and which not, using a filter provider.
In this Hands-on Lab, you will learn how to:
You must have the following items to complete this lab:
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:
Estimated time to complete this lab: 30 minutes.