// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Web.Http.Metadata; using System.Web.Http.ModelBinding; using System.Web.Http.Properties; using System.Web.Http.Validation; namespace System.Web.Http.Controllers { /// /// Extension methods for . /// [EditorBrowsable(EditorBrowsableState.Never)] public static class HttpActionContextExtensions { /// /// Gets the instance for a given . /// /// The context. /// An instance. public static ModelMetadataProvider GetMetadataProvider(this HttpActionContext actionContext) { if (actionContext == null) { throw Error.ArgumentNull("actionContext"); } return actionContext.ControllerContext.Configuration.Services.GetModelMetadataProvider(); } /// /// Gets the collection of registered instances. /// /// The context. /// A collection of instances. public static IEnumerable GetValidatorProviders(this HttpActionContext actionContext) { if (actionContext == null) { throw Error.ArgumentNull("actionContext"); } return actionContext.ControllerContext.Configuration.Services.GetModelValidatorProviders(); } /// /// Gets the collection of registered instances. /// /// The context. /// The metadata. /// A collection of registered instances. public static IEnumerable GetValidators(this HttpActionContext actionContext, ModelMetadata metadata) { if (actionContext == null) { throw Error.ArgumentNull("actionContext"); } IEnumerable validatorProviders = GetValidatorProviders(actionContext); return validatorProviders.SelectMany(provider => provider.GetValidators(metadata, validatorProviders)); } /// /// Gets the for this . /// /// The action context. /// The binding context. /// When this method returns, the value associated with the specified binding context, if the context is found; otherwise, the default value for the type of the value parameter. /// true if was present; otherwise false. public static bool TryGetBinder(this HttpActionContext actionContext, ModelBindingContext bindingContext, out IModelBinder binder) { if (actionContext == null) { throw Error.ArgumentNull("actionContext"); } if (bindingContext == null) { throw Error.ArgumentNull("bindingContext"); } binder = null; ModelBinderProvider providerFromAttr; if (ModelBindingHelper.TryGetProviderFromAttributes(bindingContext.ModelType, out providerFromAttr)) { binder = providerFromAttr.GetBinder(actionContext, bindingContext); } else { binder = actionContext.ControllerContext.Configuration.Services.GetModelBinderProviders() .Select(p => p.GetBinder(actionContext, bindingContext)) .Where(b => b != null) .FirstOrDefault(); } return binder != null; } /// /// Gets the for this . /// /// The execution context. /// The binding context. /// The . public static IModelBinder GetBinder(this HttpActionContext actionContext, ModelBindingContext bindingContext) { if (actionContext == null) { throw Error.ArgumentNull("actionContext"); } IModelBinder binder; if (TryGetBinder(actionContext, bindingContext, out binder)) { return binder; } throw Error.InvalidOperation(SRResources.ModelBinderProviderCollection_BinderForTypeNotFound, bindingContext.ModelType); } } }