This topic has not yet been rated - Rate this topic

ActionFilterAttribute Class

Represents the base class for filter attributes.

System.Object
  System.Attribute
    System.Web.Mvc.FilterAttribute
      System.Web.Mvc.ActionFilterAttribute
        System.Web.Mvc.AsyncTimeoutAttribute
        System.Web.Mvc.OutputCacheAttribute

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, Inherited = true, 
	AllowMultiple = false)]
public abstract class ActionFilterAttribute : FilterAttribute, 
	IActionFilter, IResultFilter

The ActionFilterAttribute type exposes the following members.

  Name Description
Protected method ActionFilterAttribute Initializes a new instance of the ActionFilterAttribute class.
Top
  Name Description
Public property AllowMultiple Gets or sets a value that indicates whether more than one instance of the filter attribute can be specified. (Inherited from FilterAttribute.)
Public property Order Gets or sets the order in which the action filters are executed. (Inherited from FilterAttribute.)
Public property TypeId (Inherited from Attribute.)
Top
  Name Description
Public method Equals (Inherited from Attribute.)
Protected method Finalize (Inherited from Object.)
Public method GetHashCode (Inherited from Attribute.)
Public method GetType (Inherited from Object.)
Public method IsDefaultAttribute (Inherited from Attribute.)
Public method Match (Inherited from Attribute.)
Protected method MemberwiseClone (Inherited from Object.)
Public method OnActionExecuted Called by the ASP.NET MVC framework after the action method executes.
Public method OnActionExecuting Called by the ASP.NET MVC framework before the action method executes.
Public method OnResultExecuted Called by the ASP.NET MVC framework after the action result executes.
Public method OnResultExecuting Called by the ASP.NET MVC framework before the action result executes.
Public method ToString (Inherited from Object.)
Top
  Name Description
Explicit interface implemetation Private method _Attribute.GetIDsOfNames (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.GetTypeInfo (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.GetTypeInfoCount (Inherited from Attribute.)
Explicit interface implemetation Private method _Attribute.Invoke (Inherited from Attribute.)
Top

Typically, you create an action or response filter by creating an attribute class that inherits from the abstract ActionFilterAttribute class. Some built-in action filters, such as AuthorizeAttribute and HandleErrorAttribute, inherit from the FilterAttribute class. Other action filters, such as OutputCacheAttribute, inherit from the abstract ActionFilterAttribute class, which enables the action filter to run either before or after the action method runs.

You can use filter attributes to mark any action method or controller. If the attribute marks a controller, the filter applies to all action methods in that controller.

For more information about using attributes, see Extending Metadata Using Attributes.

The following example shows how to create a simple action filter that logs trace messages before and after an action method is called.


public class LoggingFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Trace.Write("(Logging Filter)Action Executing: " +
            filterContext.ActionDescriptor.ActionName);

        base.OnActionExecuting(filterContext);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext.Exception != null)
            filterContext.HttpContext.Trace.Write("(Logging Filter)Exception thrown");

        base.OnActionExecuted(filterContext);
    }
}


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)