// 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 System; using Microsoft.AspNet.Mvc.Core; namespace Microsoft.AspNet.Mvc { /// /// An attribute which specifies a required route value for an action or controller. /// /// When placed on an action, the route data of a request must match the expectations of the route /// constraint in order for the action to be selected. See for /// the expectations that must be satisfied by the route data. /// /// When placed on a controller, unless overridden by the action, the constraint applies to all /// actions defined by the controller. /// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)] public abstract class RouteConstraintAttribute : Attribute, IRouteConstraintProvider { /// /// Creates a new . /// /// The route value key. /// /// The value. Must be /// or . /// protected RouteConstraintAttribute( [NotNull] string routeKey, RouteKeyHandling keyHandling) { RouteKey = routeKey; RouteKeyHandling = keyHandling; if (keyHandling != RouteKeyHandling.CatchAll && keyHandling != RouteKeyHandling.DenyKey) { var message = Resources.FormatRouteConstraintAttribute_InvalidKeyHandlingValue( Enum.GetName(typeof(RouteKeyHandling), RouteKeyHandling.CatchAll), Enum.GetName(typeof(RouteKeyHandling), RouteKeyHandling.DenyKey)); throw new ArgumentException(message, "keyHandling"); } } /// /// Creates a new with /// set to . /// /// The route value key. /// The expected route value. /// /// Set to true to negate this constraint on all actions that do not define a behavior for this route key. /// protected RouteConstraintAttribute( [NotNull]string routeKey, [NotNull]string routeValue, bool blockNonAttributedActions) { RouteKey = routeKey; RouteValue = routeValue; BlockNonAttributedActions = blockNonAttributedActions; } /// public string RouteKey { get; private set; } /// public RouteKeyHandling RouteKeyHandling { get; private set; } /// public string RouteValue { get; private set; } /// public bool BlockNonAttributedActions { get; private set; } } }