2014年5月17日 星期六

[.NET] Attributes特性筆記

Attributes特性
[AcceptVerbs(…)]
To specify HTTP verbs an action method will respond to.
要指定HTTP動詞的將響應的一個操作方法。
[ActionName(…)]
To define the name of an action
定義一個動作的名稱
[AdditionalMetadata(…)]
to populate the ModelMetadata.AdditionalValues​​ dictionary for a model property.
填充一個模型屬性的ModelMetadata.AdditionalValues​​字典。
[AllowHtml]
To allow HTML markup during model binding by skipping request validation for the property.
允許HTML標記在模型綁定通過跳過屬性的請求驗證。
[AsyncTimeout(…)]
To set the timeout value, in milliseconds, for an asynchronous method.
要設置超時值,以毫秒為單位的異步方法。
[Authorize(…)]
To restrict access by callers to an action method.
調用者的操作方法來限制訪問。
[ChildActionOnly]
To indicate that an action method should be called only as a child action.
要表明,作為一個的操作方法只應被子動作調用。
[HandleError(...)]
to handle an exception that is thrown by an action method.
處理異常就是將一個操作方法拋出。
[HiddenInput(…)]
To render a property or field value as a hidden input element.
為了呈現一個隱藏的輸入元素屬性或字段的值。
[HttpDelete]
to restrict an action method to handle only HTTP DELETE requests.
限制一個動作方法只處理HTTP DELETE請求。
[HttpGet]
to restrict an action method to handle only HTTP Get requests.
限制一個動作方法只處理HTTP GET請求。
[HttpPost]
to restrict an action method to handle only HTTP Post requests.
限制一個動作方法只處理HTTP POST請求。
[HttpPut]
to restrict an action method to handle only HTTP Put requests.
限制一個動作方法只處理HTTP PUT請求。
[ModelBinder(..)]
to associate a model type to a model-builder type.
關聯模型類型的模型生成器類型。
[NoAsyncTimeout]
To set the timeout to infinite (never).
要設置超時無限(從不)。
[NonAction]
To indicate that a controller method is not an action method.
為了表明控制器方法不是一個操作方法。
[OutputCache(..)]
To mark an action method whose output will be cached.
為了標記其產出將是緩存的一個操作方法。
[Remote(…)]
It is used to invoke the server-based validation from client side.
它是用於從客戶端調用基於服務器的驗證。
[RequireHttps]
It forces an unsecured HTTP request to be re-sent over HTTPS.
它迫使重新通過HTTPS發送不安全的HTTP請求。
[SessionState(…)]
Specifies the session state of the controller.
指定控制器的會話狀態。
[ValidateInput(...)]
To mark action methods whose input must be validated.
為了標記一個動作方法的輸入必須進行驗證。





sample


using System;
namespace AttTargsCS
{

// 該Attribute只對類有效.
[AttributeUsage(AttributeTargets.Class)]
public class ClassTargetAttribute : Attribute
{
}


// 該Attribute只對方法有效.
[AttributeUsage(AttributeTargets.Method)]
public class MethodTargetAttribute : Attribute
{
}


// 該Attribute只對構造器有效。
[AttributeUsage(AttributeTargets.Constructor)]
public class ConstructorTargetAttribute : Attribute
{
}


// 該Attribute只對字段有效.
[AttributeUsage(AttributeTargets.Field)]
public class FieldTargetAttribute : Attribute
{
}


// 該Attribute對類或者方法有效(組合).
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
public class ClassMethodTargetAttribute : Attribute
{
}


// 該Attribute對所有的元素有效.
[AttributeUsage(AttributeTargets.All)]
public class AllTargetsAttribute : Attribute
{
}

//上面定義的Attribute施加到程序元素上的用法
[ClassTarget] //施加到類
[ClassMethodTarget]//施加到類
[AllTargets] //施加到類
public class TestClassAttribute
{
[ConstructorTarget] //施加到構造器
[AllTargets] //施加到構造器
TestClassAttribute()
{
}

[MethodTarget] //施加到方法
[ClassMethodTarget] //施加到方法
[AllTargets] //施加到方法
public void Method1()
{
}
 
[FieldTarget] //施加到字段
[AllTargets] //施加到字段
public int myInt;

static void Main(string[] args)
{
}
}
}








0 意見:

張貼留言

 

MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template