DOCX

introduction to ASPNET MVC Global And Dynamic Action Filters

By Matthew Grant,2014-09-20 15:11
7 views 0
introduction to ASPNET MVC Global And Dynamic Action Filters

Hands-On Lab

    ASP.NET MVC Global & Dynamic Action

    Filters

Lab version: 1.1.0

    Last updated: 9/20/2011

    Page 1

CONTENTS

    OVERVIEW ............................................................................................................................................. 3 EXERCISE 1: CREATING A GLOBAL ACTION FILTER ........................................................................ 4

    Task 1 Registering a Filter as Global............................................................................................... 4

    Task 2 Running the Application ..................................................................................................... 5 EXERCISE 2: CREATING A GLOBAL DYNAMIC FILTER ..................................................................... 6

    Task 1 Creating a Filter Provider .................................................................................................... 6

    Task 2 Registering a Global Filter ................................................................................................. 10

    Task 3 Running the Application ................................................................................................... 11 SUMMARY ............................................................................................................................................ 12

    Page 2

Overview

    Note: 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 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:

    ; Create a global filter to apply the logging of actions to all controllers in application.

    ; Create a filter provider to apply logging to some controllers or actions, excluding others.

System Requirements

    You must have the following items to complete this lab:

    ; ASP.NET and ASP.NET MVC 3

    ; Visual Studio 2010 Express

    ; SQL Server Database (Express edition or above)

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

    3.0: http://go.microsoft.com/fwlink/?LinkID=194638.

    Page 3

Setup

    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.

Exercises

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

    1. Exercise 1: Creating a Global Action Filter

    2. Exercise 2: Creating a Global Dynamic Filter

Estimated time to complete this lab: 30 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: Creating a Global Action Filter

    Exercise 1: Creating a Global Action Filter

    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.

    1. Open the begin solution MvcMusicStore.sln at Source\Ex01-Global Action Filter\Begin.

    Page 4

    2. Open Global.asax.cs at project root. In the method RegisterGlobalFilters , add a new security

    filter to the filters collection:

    C#

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)

    {

     filters.Add(new HandleErrorAttribute());

     filters.Add(new ActionLogFilterAttribute());

    }

    Note: 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

    1. Press F5 to run the application.

    2. Browse to /ActionLog View. You will see that the initial activities have been tracked.

    Figure 1

    ActionLogView: Initial site activity was tracked

3. Click on Admin to load the LogOn View.

    4. Browse /ActionLog view again to see the administration site tracked. If you see the same table

    as before, please refresh the page.

    Page 5

    Figure 2

    ActionLogView: Recent activities logged

Next Step

    Exercise 2: Creating a Global Dynamic Filter

    Exercise 2: Creating a Global Dynamic Filter

    In this exercise, you will learn how to create a global dynamic filter that will get executed or not depending on the context. This means that you could set any filter as global, but use it only for the controllers or actions that satisfy a certain condition. For that purpose you will implement GetFilters

    method in your custom filter provider to add your own logic behind the filter retrieval. Task 1 Creating a Filter Provider

    In this task, you learn how custom Filter Providers manage the global behavior of any MVC Filter. You will create a custom Filter Provider that stores the controller actions to log and applies the filter when an action is in the list.

    1. Open Begin solution from \Ex02 Global Dynamic Filter\Begin.

    2. Create a new C# class in Filters folder and rename it ActionLogFilterProvider.cs.

    Page 6

    3. Add the reference to the System.Web.Mvc namespace. C#

    using System;

    using System.Collections.Generic; using System.Linq;

    using System.Web;

    using System.Web.Mvc;

    namespace MvcMusicStore.Filters {

     public class ActionLogFilterProvider

     {

     }

    }

    4. Make ActionLogFilterProvider implement the interface IFilterProvider. C#

    public class ActionLogFilterProvider : IFilterProvider

    5. Define the internal class ControllerAction inmediatly after ActionLogFilterProvider class

    definition that will have a controller and action names as the members.

    (Code Snippet ASP.NET Global and Dynamic Action Filters Ex2 ControllerAction CSharp)

    C#

    using System;

    using System.Collections.Generic; using System.Linq;

    using System.Web;

    using System.Web.Mvc;

    namespace MvcMusicStore.Filters {

     public class ActionLogFilterProvider

     {

     }

     internal class ControllerAction

     {

     internal string ControllerName { get; set; }

     internal string ActionName { get; set; }

     }

    }

    Page 7

    6. Then define a list of ControllerAction elements as new ActionLogFilter property: (Code Snippet ASP.NET Global and Dynamic Action Filters Ex2 Actions List CSharp) C#

using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.Mvc;

namespace MvcMusicStore.Filters

    {

     public class ActionLogFilterProvider : IFilterProvider

     {

     private IList actions = new List();

     }

     internal class ControllerAction

     {

     internal string ControllerName { get; set; }

     internal string ActionName { get; set; }

     }

    }

    7. Create an Add method to ActionLogFilterProvider class to store in the list all the controllers and

    actions that will be logged:

    (Code Snippet ASP.NET Global and Dynamic Action Filters Ex2 Add Method CSharp) C#

    

    private IList actions = new List();

public void Add(string controllername, string actionname)

    {

     actions.Add(new ControllerAction() { ControllerName = controllername,

    ActionName = actionname });

    }

    

    8. Implement in ActionLogFilterProvider class the method GetFilters that will read the list of

    controller actions and evaluate which ones apply to the filter.

    (Code Snippet ASP.NET Global and Dynamic Action Filters Ex2 GetFilters CSharp) C#

    Page 8

    public IEnumerable GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)

    {

     foreach (ControllerAction action in actions)

     if ((action.ControllerName ==

    actionDescriptor.ControllerDescriptor.ControllerName || action.ControllerName

    == "*")

     && (action.ActionName == actionDescriptor.ActionName || action.ActionName == "*")) {

     yield return new Filter(new ActionLogFilterAttribute(), FilterScope.First, null);

     break;

     }

     yield break;

    }

Note: This implementation of GetFilters method checks if the controller name and the action

    name are in the list and returns a new instance of the custom action filter class

    ActionLogFilterAttribute. You could also use the Controller Context parameter to add more complex logic to this method.

    The "*" will be used as a wildcard for not filtering by a controller name or an action name. You

    can find ControllerContext class reference in this article at msdn.

    9. Your filter provider class should finally look like this: C#

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Web;

    using System.Web.Mvc;

namespace MvcMusicStore.Filters

    {

     public class ActionLogFilterProvider : IFilterProvider

     {

     private IList<ControllerAction> actions = new List<ControllerAction>();

     public void Add(string controllername, string actionname)

     {

     actions.Add(new ControllerAction() { ControllerName =

    controllername, ActionName = actionname });

     }

    Page 9

     public IEnumerable GetFilters(ControllerContext

    controllerContext, ActionDescriptor actionDescriptor)

     {

     foreach (ControllerAction action in actions)

     if ((action.ControllerName ==

    actionDescriptor.ControllerDescriptor.ControllerName || action.ControllerName

    == "*")

     && (action.ActionName == actionDescriptor.ActionName ||

    action.ActionName == "*"))

     {

     yield return new Filter(new ActionLogFilterAttribute(), FilterScope.First, null);

     break;

     }

     yield break;

     }

     }

     internal class ControllerAction

     {

     internal string ControllerName { get; set; }

     internal string ActionName { get; set; }

     }

    }

In the following step you will add this provider to the global filter provider collection.

    Task 2 Registering a Global Filter

    In this task, you will register a custom filter provider as global in the Global.asax.cs.

    1. Add an instance of your filter provider into MVC FilterProvider collection, in the method RegisterGlobalFilters at Global.asax.cs class:

    (Code Snippet ASP.NET Global and Dynamic Action Filters Ex2 LogProviders CSharp) C#

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)

    {

     filters.Add(new HandleErrorAttribute());

     ActionLogFilterProvider provider = new ActionLogFilterProvider();

     provider.Add("Account", "LogOn");

     provider.Add("Store", "*");

     provider.Add("*", "Index");

     FilterProviders.Providers.Add(provider); }

    Page 10

Report this document

For any questions or suggestions please email
cust-service@docsford.com