ActionFilterAttribute Class

Represents the base class for filter attributes.

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method, Inherited := True,  _
	AllowMultiple := False)> _
Public MustInherit Class ActionFilterAttribute _
	Inherits FilterAttribute _
	Implements IActionFilter, IResultFilter
'Usage
Dim instance As ActionFilterAttribute

The ActionFilterAttribute type exposes the following members.

  NameDescription
Protected methodActionFilterAttributeInitializes a new instance of the ActionFilterAttribute class.
Top

  NameDescription
Public propertyAllowMultipleGets or sets a value that indicates whether more than one instance of the filter attribute can be specified. (Inherited from FilterAttribute.)
Public propertyOrderGets or sets the order in which the action filters are executed. (Inherited from FilterAttribute.)
Public propertyTypeId (Inherited from Attribute.)
Top

  NameDescription
Public methodEquals (Inherited from Attribute.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Attribute.)
Public methodGetType (Inherited from Object.)
Public methodIsDefaultAttribute (Inherited from Attribute.)
Public methodMatch (Inherited from Attribute.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodOnActionExecutedCalled by the ASP.NET MVC framework after the action method executes.
Public methodOnActionExecutingCalled by the ASP.NET MVC framework before the action method executes.
Public methodOnResultExecutedCalled by the ASP.NET MVC framework after the action result executes.
Public methodOnResultExecutingCalled by the ASP.NET MVC framework before the action result executes.
Public methodToString (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNames (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfo (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCount (Inherited from Attribute.)
Explicit interface implemetationPrivate 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
    Inherits ActionFilterAttribute

    Public Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)
        filterContext.HttpContext.Trace.Write("(Logging Filter)Action Executing: " + _
                filterContext.ActionDescriptor.ActionName)

        MyBase.OnActionExecuting(filterContext)
    End Sub

    Public Overrides Sub OnActionExecuted(ByVal filterContext As ActionExecutedContext)
        If Not filterContext.Exception Is Nothing Then
            filterContext.HttpContext.Trace.Write("(Logging Filter)Exception thrown")
        End If

        MyBase.OnActionExecuted(filterContext)
    End Sub
End Class


Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: