In this exercise, you will learn how to add a global filter that will perform certain logic before displaying each view. In order to do that, you will register as global the custom filter for application logging created in ASP.NET MVC Custom Action Filter Hands-on Lab.
Task 1 – Registering a Filter as Global
In this task, you will register a global filter to manage your site logging at a global level. In order to do that, you will add ActionLogFilterAttribute filter into MVC GlobalFilterCollection at Global.asax.[cs|vb].
Open the begin solution MvcMusicStore.sln at Source\Ex01-Global Action Filter\Begin.
Open Global.asax.[cs|vb] at project root. In the method RegisterGlobalFilters , add a new security filter to the filters collection:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
Public Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
the previous implementation of RegisterGlobalFilters (with the HandleErrorAttribute filter only) is the one included in an MVC 3 application by default. In this task, you are only adding an additional filter, ActionLogFilterAttribute.
Task 2 – Running the Application
Press F5 to run the application.
Browse to /ActionLog View. You will see that the initial activities have been tracked.
ActionLogView: Initial site activity was tracked
Click on Admin to load the LogOn View.
Browse /ActionLog view again to see the administration site tracked. If you see the same table as before, please refresh the page.