[ASP.Net MVC 类库探索] AcceptVerbsAttribute 类

类签名

[AttributeUsageAttribute(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AcceptVerbsAttribute : ActionMethodSelectorAttribute


这是个特性类,可以作为特性修饰Action方法,这个特性指定了可以访问Action方法的HttpMethod方法(HttpMethod也被称为HttpVerb),常见的HttpMehod有GET和POST,如果访问Action的HttpMethod不在特性指定的范围内,将返回HTTP 404错误

 

从这个类的源代码中可以看出,该类有两个构造函数,分别是接受HttpVerb枚举和一组变参String,其中HttpVerb枚举是Flag特性的,也就是说可以使用"|"操作符来指定多个参数。

例如让某个Action可同时接受Get和Post的Http访问,可以按下面的代码实现:

 

[AcceptVerbs(HttpVerbs.Post|HttpVerbs.Get)]
        public ActionResult About()
        {
            
            return View();
        }


 

也可以按下面的代码实现:

 

[AcceptVerbs("POST","GET")]
        public ActionResult About()
        {
            
            return View();
        }

 

下面是摘自CodePlex的该类的源代码:

 

/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. All rights reserved.
 *
 * This software is subject to the Microsoft Public License (Ms-PL). 
 * A copy of the license can be found in the license.htm file included 
 * in this distribution.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * ***************************************************************************/

namespace System.Web.Mvc {
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Linq;
    using System.Reflection;
    using System.Web.Mvc.Resources;

    [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.HttpMethod;
            return Verbs.Contains(incomingVerb, StringComparer.OrdinalIgnoreCase);
        }
    }
}



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值