1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\r
5 * This software is subject to the Microsoft Public License (Ms-PL).
\r
6 * A copy of the license can be found in the license.htm file included
\r
7 * in this distribution.
\r
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
15 using System.Collections.Generic;
\r
16 using System.Collections.ObjectModel;
\r
17 using System.Diagnostics.CodeAnalysis;
\r
19 using System.Reflection;
\r
20 using System.Web.Mvc.Resources;
\r
22 [SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments",
\r
23 Justification = "The accessor is exposed as an ICollection<string>.")]
\r
24 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
\r
25 public sealed class AcceptVerbsAttribute : ActionMethodSelectorAttribute {
\r
26 public AcceptVerbsAttribute(HttpVerbs verbs)
\r
27 : this(EnumToArray(verbs)) {
\r
30 public AcceptVerbsAttribute(params string[] verbs) {
\r
31 if (verbs == null || verbs.Length == 0) {
\r
32 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "verbs");
\r
35 Verbs = new ReadOnlyCollection<string>(verbs);
\r
38 public ICollection<string> Verbs {
\r
43 private static void AddEntryToList(HttpVerbs verbs, HttpVerbs match, List<string> verbList, string entryText) {
\r
44 if ((verbs & match) != 0) {
\r
45 verbList.Add(entryText);
\r
49 internal static string[] EnumToArray(HttpVerbs verbs) {
\r
50 List<string> verbList = new List<string>();
\r
52 AddEntryToList(verbs, HttpVerbs.Get, verbList, "GET");
\r
53 AddEntryToList(verbs, HttpVerbs.Post, verbList, "POST");
\r
54 AddEntryToList(verbs, HttpVerbs.Put, verbList, "PUT");
\r
55 AddEntryToList(verbs, HttpVerbs.Delete, verbList, "DELETE");
\r
56 AddEntryToList(verbs, HttpVerbs.Head, verbList, "HEAD");
\r
58 return verbList.ToArray();
\r
61 public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) {
\r
62 if (controllerContext == null) {
\r
63 throw new ArgumentNullException("controllerContext");
\r
66 string incomingVerb = controllerContext.HttpContext.Request.GetHttpMethodOverride();
\r
68 return Verbs.Contains(incomingVerb, StringComparer.OrdinalIgnoreCase);
\r