Attributes and ASP.NET MVC
- 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?
- An attribute is a class that inherits from the abstract class
- System.Attribute
- . 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
- System.Web.Mvc.AuthorizeAttribute
- to a controller’s action method, you would use the following syntax:
- C# example
-
[Authorize] public ActionResult Index() { }
Visual Basic example
<Authorize()> _
Function Index() As ActionResult
End Function
Attribute Parameters
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,
has a constructor that takes a name parameter.
C# example
-
[ActionName("Start")] public ActionResult Index() { }
Visual Basic example
<ActionName("Start")> _
Function Index() As ActionResult
End FunctionA named parameter corresponds to a public property or public field of an attribute. For example,
has several public properties (named parameters).
- C# example
- [OutputCache(CacheProfile = "MyProfile", Duration = 10)]
- public ActionResult Index() { }
- Visual Basic example
- <OutputCache(CacheProfile := "MyProfile", Duration := 10)> _
- Function Index() As ActionResult
- EndFunction
Customizing Your Own Attribute
- 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
- System.Web.Mvc.ActionFilterAttribute
- to implement an action filter. The following class implements a simple logging action filter:
- C# example
- 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);
- }
- }
- 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