ASP.NET MVC ActionMethodSelectorAttribute 以及HttpGet等Action特性
一、ActionMethodSelectorAttribute
其是一個(gè)抽象類(lèi),繼承自Attribute,子類(lèi)有NonActionAttribute、HttpGetAttribute、HttpPostAttribute、HttpPutAttribute、HttpDeleteAttribute、HttpPatchAttribute、HttpHeadAttribute、HttpOptionsAttribute和AcceptVerbsAttribute,其唯一抽象方法IsValidForRequest,如果返回false,結(jié)果會(huì)提示Action Not Found
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public abstract class ActionMethodSelectorAttribute : Attribute {public abstract bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo); } AcceptVerbsAttribute 直接繼承 ActionMethodSelectorAttribute [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]public sealed class AcceptVerbsAttribute : ActionMethodSelectorAttribute{public AcceptVerbsAttribute(HttpVerbs verbs): this(EnumToArray(verbs)){}public AcceptVerbsAttribute(params string[] verbs){if (verbs == null || verbs.Length == 0){throw new ArgumentException(MvcResources.Common_NullOrEmpty, "verbs");}Verbs = new ReadOnlyCollection<string>(verbs);}public ICollection<string> Verbs { get; private set; }private static void AddEntryToList(HttpVerbs verbs, HttpVerbs match, List<string> verbList, string entryText){if ((verbs & match) != 0){verbList.Add(entryText);}}internal static string[] EnumToArray(HttpVerbs verbs){List<string> verbList = new List<string>();AddEntryToList(verbs, HttpVerbs.Get, verbList, "GET");AddEntryToList(verbs, HttpVerbs.Post, verbList, "POST");AddEntryToList(verbs, HttpVerbs.Put, verbList, "PUT");AddEntryToList(verbs, HttpVerbs.Delete, verbList, "DELETE");AddEntryToList(verbs, HttpVerbs.Head, verbList, "HEAD");AddEntryToList(verbs, HttpVerbs.Patch, verbList, "PATCH");AddEntryToList(verbs, HttpVerbs.Options, verbList, "OPTIONS");return verbList.ToArray();}public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo){if (controllerContext == null){throw new ArgumentNullException("controllerContext");}string incomingVerb = controllerContext.HttpContext.Request.GetHttpMethodOverride();return Verbs.Contains(incomingVerb, StringComparer.OrdinalIgnoreCase);}}除了NonActionAttribute,內(nèi)部都是通過(guò)AcceptVerbsAttribute 來(lái)實(shí)現(xiàn)的,如HttpGetAttribute,其他都類(lèi)似
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]public sealed class HttpGetAttribute : ActionMethodSelectorAttribute{private static readonly AcceptVerbsAttribute _innerAttribute = new AcceptVerbsAttribute(HttpVerbs.Get);public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo){return _innerAttribute.IsValidForRequest(controllerContext, methodInfo);}}NonActionAttribute,IsValidForRequest直接返回false
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]public sealed class NonActionAttribute : ActionMethodSelectorAttribute{public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo){return false;}}二、ActionNameSelectorAttribute
其是一個(gè)抽象類(lèi),繼承自Attribute,子類(lèi)有ActionNameAttribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]public abstract class ActionNameSelectorAttribute : Attribute{public abstract bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo);} [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]public sealed class ActionNameAttribute : ActionNameSelectorAttribute{public ActionNameAttribute(string name){if (String.IsNullOrEmpty(name)){throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");}Name = name;}public string Name { get; private set; }public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo){//只是驗(yàn)證根據(jù)請(qǐng)求進(jìn)行路由匹配出的actionName,是否和ActionName特性上指定的Name相等return String.Equals(actionName, Name, StringComparison.OrdinalIgnoreCase);}}
三、自定義ActionMethodSelectorAttribute
驗(yàn)證請(qǐng)求是GET而且是ajax的
public class MyActionMethodSelectorAttribute : ActionMethodSelectorAttribute{public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo){//get/poststring httpMethodOverride = controllerContext.HttpContext.Request.GetHttpMethodOverride();//isAjaxvar isAjax = controllerContext.HttpContext.Request.IsAjaxRequest();var b = httpMethodOverride.ToLower() == "get" && isAjax;return b;}}?
轉(zhuǎn)載于:https://www.cnblogs.com/shawnhu/p/8401338.html
總結(jié)
以上是生活随笔為你收集整理的ASP.NET MVC ActionMethodSelectorAttribute 以及HttpGet等Action特性的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 简单而又成熟男生网名127个
- 下一篇: 【从零开始搭建自己的.NET Core