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使用和扩展

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

转载于:https://www.cnblogs.com/miku/archive/2013/01/16/2862476.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值