Share via


Attributes and ASP.NET MVC

  1. Anyone who wants to learn about ASP.NET MVC will quickly discover that MVC makes extensive use of a relatively new feature of .NET called attributes. Many programmers who are new to ASP.NET MVC may also be new to the use of attributes. This article covers the basic concepts of attributes and their use in ASP.NET MVC.

 What is an Attribute?

  1. An attribute is a class that inherits from the abstract class
  2. System.Attribute
  3. . By convention, all attributes are given a class name that ends with the word “Attribute”. The .NET framework recognizes this convention and allows you to drop “Attribute” from the syntax for attaching the attribute. For example, to attach
  4. System.Web.Mvc.AuthorizeAttribute
  5. to a controller’s action method, you would use the following syntax: 
  1. C# example
  1.  [Authorize]
    public ActionResult Index() { }
    
  2. Visual Basic example

  3. <Authorize()> _
    Function Index() As ActionResult
    End Function

 Attribute Parameters

  1. An attribute can take a parameter that is either positional or named. A positional parameter corresponds to the parameters of the attribute’s public constructors. For example,

  2. System.Web.Mvc.ActionNameAttribute

  3. has a constructor that takes a name parameter.

  4. C# example

  5.  [ActionName("Start")]
    public ActionResult Index() { }
    
  6.  Visual Basic example

  7. <ActionName("Start")> _
    Function Index() As ActionResult
    End Function

  8.  A named parameter corresponds to a public property or public field of an attribute. For example,

  9. System.Web.Mvc.OutputCacheAttribute

  10. has several public properties (named parameters).

  1. C# example
  1. [OutputCache(CacheProfile = "MyProfile", Duration = 10)]
  2. public ActionResult Index() { } 
  1. Visual Basic example
  1. <OutputCache(CacheProfile := "MyProfile", Duration := 10)> _
  2. Function Index() As ActionResult
  3. EndFunction

 Customizing Your Own Attribute

  1. You can write your own attribute by creating a class that inherits either directly or indirectly from System.Attribute. ASP.NET MVC has several abstract attributes that you are intended to customize. For example, you must customize
  2. System.Web.Mvc.ActionFilterAttribute
  3. to implement an action filter. The following class implements a simple logging action filter: 
  1. C# example
  1. public class LoggingFilterAttribute : ActionFilterAttribute   
  2. {
  3.     public override void OnActionExecuting(ActionExecutingContext filterContext)
  4.     {
  5.          filterContext.HttpContext.Trace.Write("(Logging Filter)Action Executing: " +
  6.          filterContext.ActionDescriptor.ActionName);
  7.          base.OnActionExecuting(filterContext);
  8.      }
  9.      public override void OnActionExecuted(ActionExecutedContext filterContext)
  10.      {
  11.          if (filterContext.Exception != null)
  12.          filterContext.HttpContext.Trace.Write("(Logging Filter)Exception thrown");
  13.          base.OnActionExecuted(filterContext);
  14.      }
  15.  }
  1. Visual Basic example
     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

 ASP.NET MVC Attributes

  • The following list shows the attributes that are currently available to you as an ASP.NET MVC programmer. All of these attributes are in the System.Web.Mvc namespace.
  • AcceptViewAttribute
  • ActionFilterAttribute
  • ActionMethodSelectorAttribute
  • ActionNameAttribute
  • ActionNameSelectorAttribute
  • AuthorizeAttribute
  • BindAttribute
  • CustomModelBinderAttribute
  • FilterAttribute
  • HandleErrorAttribute
  • HiddenInputAttribute
  • HttpDeleteAttribute
  • HttpGetAttribute
  • HttpPostAttribute
  • HttpPutAttribute
  • ModelBinderAttribute
  • NonActionAttribute
  • OutputCacheAttribute
  • RequireHttpsAttribute
  • ValidateAntiForgeryTokenAttribute
  • ValidateInputAttribute

-- Keith Newman
ASP.NET User Education
This posting is provided "AS IS" with no warranties, and confers no rights.

Give Your Feedback on the Documentation
Help us improve the developer documentation by taking the Visual Studio and .NET Framework Content Survey. This survey will give us a better understanding of the type of applications you are developing as well as how you use Help and how we can improve it. The survey takes only 10 minutes, and we appreciate your feedback.

Comments

  • Anonymous
    February 24, 2010
    I may be misunderstanding your openning, but haven't Attributes existed since 1.0 of .Net?
  • Anonymous
    February 24, 2010
    I have to admit I was similarly perplexed.
  • Anonymous
    January 10, 2011
    Are they going to introduce attributes in netfx 6.5? I cannot find them anywhere.
  • Anonymous
    December 11, 2011
    "Anyone who wants to learn about ASP.NET MVC will quickly discover that MVC makes extensive use of a relatively new feature of .NET called attributes"Completely Bizarre
  • Anonymous
    March 11, 2014
    True, Attributes have been around since 2001 but MVC has made them popular today.  Older application did not use them heavily so no one really covered Attributes but the author did an excellent job of simplifying Attribute development.
  • Anonymous
    December 03, 2014
    @Dvdmiami that's just not true; attributes have been continuously and extensive used in pretty much every area of .NET, and most use libraries