/// <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 new ArgumentNullException("controllerSettings"); } if (controllerDescriptor == null) { throw new ArgumentNullException("controllerDescriptor"); } ServicesContainer services = controllerSettings.Services; Contract.Assert(services != null); IContainerMetadata containerMetadata = controllerDescriptor.GetContainerMetadata(); // Replace the action selector with one that is based on the OData routing conventions IHttpActionSelector originalActionSelector = services.GetActionSelector(); IHttpActionSelector actionSelector; if (containerMetadata != null) { // ContainerMetadata was stored with the HttpControllerDescriptor - so use our "special" ActionSelector actionSelector = new EntityRepositoryActionSelector(containerMetadata, originalActionSelector); } else { // No ContainerMetadata stored with the HttpControllerDescriptor - so use the standard odata ActionSelector actionSelector = new ODataActionSelector(originalActionSelector); } controllerSettings.Services.Replace(typeof(IHttpActionSelector), actionSelector); }
/// <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"); } ServicesContainer services = controllerSettings.Services; Contract.Assert(services != null); // Replace the action selector with one that is based on the OData routing conventions IHttpActionSelector originalActionSelector = services.GetActionSelector(); IHttpActionSelector actionSelector = new ODataActionSelector(originalActionSelector); controllerSettings.Services.Replace(typeof(IHttpActionSelector), actionSelector); }
/// <summary> /// Enables OData support by adding an OData route and enabling OData controller and action selection, querying, and formatter support for OData. /// </summary> /// <param name="configuration">The server configuration.</param> /// <param name="model">The EDM model to use for the service.</param> /// <param name="routePrefix">The prefix to add to the OData route's path template.</param> public static void EnableOData(this HttpConfiguration configuration, IEdmModel model, string routePrefix) { if (configuration == null) { throw Error.ArgumentNull("configuration"); } if (model == null) { throw Error.ArgumentNull("model"); } // Querying configuration.SetEdmModel(model); configuration.EnableQuerySupport(); // Routing string routeTemplate = String.IsNullOrEmpty(routePrefix) ? ODataRouteConstants.ODataPathTemplate : routePrefix + "/" + ODataRouteConstants.ODataPathTemplate; IODataPathHandler pathHandler = configuration.GetODataPathHandler() ?? new DefaultODataPathHandler(model); IHttpRouteConstraint routeConstraint = new ODataPathRouteConstraint(pathHandler); configuration.Routes.MapHttpRoute(ODataRouteConstants.RouteName, routeTemplate, null, new HttpRouteValueDictionary() { { ODataRouteConstants.ConstraintName, routeConstraint } }); IEnumerable<IODataRoutingConvention> routingConventions = configuration.GetODataRoutingConventions(); IHttpControllerSelector controllerSelector = new ODataControllerSelector(routingConventions, configuration.Services.GetHttpControllerSelector()); IHttpActionSelector actionSelector = new ODataActionSelector(routingConventions, configuration.Services.GetActionSelector()); configuration.Services.Replace(typeof(IHttpControllerSelector), controllerSelector); configuration.Services.Replace(typeof(IHttpActionSelector), actionSelector); // Formatter configuration.Formatters.InsertRange(0, ODataMediaTypeFormatters.Create(model)); }