private static HttpActionBinding GetActionBinding(HttpActionDescriptor actionDescriptor) { HttpControllerDescriptor controllerDescriptor = actionDescriptor.ControllerDescriptor; if (controllerDescriptor == null) { return(null); } ServicesContainer controllerServices = controllerDescriptor.Configuration.Services; IActionValueBinder actionValueBinder = controllerServices.GetActionValueBinder(); HttpActionBinding actionBinding = actionValueBinder != null?actionValueBinder.GetBinding(actionDescriptor) : null; return(actionBinding); }
/// <summary> /// Callback invoked to set per-controller overrides for this controllerDescriptor. /// </summary> /// <param name="controllerSettings">The controller settings to initialize.</param> /// <param name="controllerDescriptor">The controller descriptor. Note that the <see /// cref="T:System.Web.Http.Controllers.HttpControllerDescriptor" /> can be associated with the derived /// controller type given that <see cref="T:System.Web.Http.Controllers.IControllerConfiguration" /> is /// inherited.</param> public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor) { if (controllerSettings == null) { throw Error.ArgumentNull("controllerSettings"); } if (controllerDescriptor == null) { throw Error.ArgumentNull("controllerDescriptor"); } // If any OData formatters are registered globally, do nothing and use those instead MediaTypeFormatterCollection controllerFormatters = controllerSettings.Formatters; if (!controllerFormatters.Where(f => f != null && Decorator.GetInner(f) is ODataMediaTypeFormatter).Any()) { // Remove Xml and Json formatters to avoid media type conflicts. RemoveFormatters(controllerFormatters, controllerFormatters.Where(f => f is XmlMediaTypeFormatter || f is JsonMediaTypeFormatter)); // Then add our formatters. controllerFormatters.InsertRange(0, CreateODataFormatters()); } ServicesContainer services = controllerSettings.Services; Contract.Assert(services != null); // Replace the action value binder with one that attaches the request to the formatter. IActionValueBinder originalActionValueBinder = services.GetActionValueBinder(); IActionValueBinder actionValueBinder = new PerRequestActionValueBinder(originalActionValueBinder); controllerSettings.Services.Replace(typeof(IActionValueBinder), actionValueBinder); // Replace the content negotiator with one that uses the per-request formatters. // This negotiator is required to allow CanReadType to have access to the model. IContentNegotiator originalContentNegotiator = services.GetContentNegotiator(); IContentNegotiator contentNegotiator = new PerRequestContentNegotiator(originalContentNegotiator); controllerSettings.Services.Replace(typeof(IContentNegotiator), contentNegotiator); }