// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNet.Routing; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; namespace Microsoft.AspNet.Mvc { public class HttpMethodConstraint : IActionConstraint { private readonly IReadOnlyList _methods; // Empty collection means any method will be accepted. public HttpMethodConstraint(IEnumerable httpMethods) { if (httpMethods == null) { throw new ArgumentNullException("httpMethods"); } var methods = new List(); foreach (var method in httpMethods) { if (string.IsNullOrEmpty(method)) { throw new ArgumentException("httpMethod cannot be null or empty"); } methods.Add(method); } _methods = new ReadOnlyCollection(methods); } public IEnumerable HttpMethods { get { return _methods; } } public bool Accept([NotNull] RouteContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (_methods.Count == 0) { return true; } var request = context.HttpContext.Request; return (HttpMethods.Any(m => m.Equals(request.Method, StringComparison.Ordinal))); } } }