zoukankan      html  css  js  c++  java
  • MVC扩展(ActionNameSelectorAttribute vs ActionMethodSelectorAttribute)

    关于ActionNameSelectorAttribute 和 ActionMethodSelectorAttribute的区别,请参考http://www.cnblogs.com/P_Chou/archive/2010/12/01/details-asp-net-mvc-07.html

    区分 [HttpPost] 和  [AcceptVerbs(HttpVerbs.Post)] .他们都是ActionMethodSelectorAttribute的子类。查看代码

    [SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments", Justification = "The accessor is exposed as an ICollection<string>.")]
        [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");
    
                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);
            }
        }
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
        public sealed class HttpPostAttribute : ActionMethodSelectorAttribute {
    
            private static readonly AcceptVerbsAttribute _innerAttribute = new AcceptVerbsAttribute(HttpVerbs.Post);
    
            public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) {
                return _innerAttribute.IsValidForRequest(controllerContext, methodInfo);
            }
        }

    可以看作

    [AcceptVerbs(HttpVerbs.Post)]

    public ActionResult MyTest() 

    完全等于

    [HttpPost]

    public ActionResult MyTest()

    但当Action同时接受2种提交方式时。就应该使用

    [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]

    public ActionResult MyTest()

    而不能用

    [HttpPost]
    [HttpGet]

    public ActionResult MyTest()

    因为一个请求不可能既是Post又是Get.

    关于ActionNameSelectorAttribute , ActionMethodSelectorAttribute使用和扩展

    这里给了详细的说明,  请参照

  • 相关阅读:
    JavaWeb学习总结(一)——使用Session防止表单重复提交
    java_socket套接字网络编程_实现多线程聊天
    服务器响应的生成:HTTP状态代码——HttpServletResponse接口应用
    服务器响应的生成:HTTP响应报头——HttpServletResponse接口的应用
    java_socket套接字网络编程
    JavaWeb学习总结(二)——JSP中的九个内置对象
    Servlet客户请求的处理:HTTP请求报头HttpServletRequest接口应用
    文件上传利器SWFUpload使用指南
    JavaScript高级特性创建对象的九种方式
    JavaScript高级特性实现继承的七种方式
  • 原文地址:https://www.cnblogs.com/miku/p/2862476.html
Copyright © 2011-2022 走看看