Remove excessive shortcut key matching in ToolStrip
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / AcceptVerbsAttribute.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\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
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Collections.ObjectModel;\r
17     using System.Diagnostics.CodeAnalysis;\r
18     using System.Linq;\r
19     using System.Reflection;\r
20     using System.Web.Mvc.Resources;\r
21 \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
28         }\r
29 \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
33             }\r
34 \r
35             Verbs = new ReadOnlyCollection<string>(verbs);\r
36         }\r
37 \r
38         public ICollection<string> Verbs {\r
39             get;\r
40             private set;\r
41         }\r
42 \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
46             }\r
47         }\r
48 \r
49         internal static string[] EnumToArray(HttpVerbs verbs) {\r
50             List<string> verbList = new List<string>();\r
51 \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
57 \r
58             return verbList.ToArray();\r
59         }\r
60 \r
61         public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) {\r
62             if (controllerContext == null) {\r
63                 throw new ArgumentNullException("controllerContext");\r
64             }\r
65 \r
66             string incomingVerb = controllerContext.HttpContext.Request.GetHttpMethodOverride();\r
67 \r
68             return Verbs.Contains(incomingVerb, StringComparer.OrdinalIgnoreCase);\r
69         }\r
70     }\r
71 }\r