Thứ Hai, 8 tháng 12, 2014

Attributes and 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, System.Web.Mvc.ActionNameAttribute 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 Function

     A named parameter corresponds to a public property or public field of an attribute. For example, System.Web.Mvc.OutputCacheAttribute 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