ASP.NET MVC ActionMethodSelectorAttribute 以及HttpGet等Action特性
一、ActionMethodSelectorAttribute
其是一个抽象类,继承自Attribute,子类有NonActionAttribute、HttpGetAttribute、HttpPostAttribute、HttpPutAttribute、HttpDeleteAttribute、HttpPatchAttribute、HttpHeadAttribute、HttpOptionsAttribute和AcceptVerbsAttribute,其唯一抽象方法IsValidForRequest,如果返回false,结果会提示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,内部都是通过AcceptVerbsAttribute 来实现的,如HttpGetAttribute,其他都类似
[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
其是一个抽象类,继承自Attribute,子类有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) {
//只是验证根据请求进行路由匹配出的actionName,是否和ActionName特性上指定的Name相等 return String.Equals(actionName, Name, StringComparison.OrdinalIgnoreCase); } }
三、自定义ActionMethodSelectorAttribute
验证请求是GET而且是ajax的
public class MyActionMethodSelectorAttribute : ActionMethodSelectorAttribute { public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo) { //get/post string httpMethodOverride = controllerContext.HttpContext.Request.GetHttpMethodOverride(); //isAjax var isAjax = controllerContext.HttpContext.Request.IsAjaxRequest(); var b = httpMethodOverride.ToLower() == "get" && isAjax; return b; } }