Exemple #1
0
        /// <summary>
        /// Creates an action selector, descriptors and route builder based on
        /// the specified controller
        /// </summary>
        /// <param name="controllerType"></param>
        /// <param name="actionName">Action name of the action descriptors to return</param>
        /// <param name="routeBuilder"></param>
        /// <param name="actionSelector"></param>
        /// <param name="actionDescriptors"></param>
        public static void SetupActionSelector(System.Type controllerType,
                                               string actionName,
                                               out IRouteBuilder routeBuilder,
                                               out ODataActionSelector actionSelector,
                                               out IReadOnlyList <ControllerActionDescriptor> actionDescriptors)

        {
            routeBuilder      = RoutingConfigurationFactory.Create();
            actionDescriptors = ControllerDescriptorFactory.Create(routeBuilder, controllerType.Name, controllerType)
                                as IReadOnlyList <ControllerActionDescriptor>;
            actionDescriptors = actionDescriptors.Where(a => a.ActionName == actionName).ToList();
            var serviceProvider = routeBuilder.ServiceProvider;

#if NETCOREAPP2_0
            var actionsProvider           = serviceProvider.GetRequiredService <IActionDescriptorCollectionProvider>();
            var actionConstraintsProvider = serviceProvider.GetRequiredService <ActionConstraintCache>();
            var loggerFactory             = serviceProvider.GetRequiredService <ILoggerFactory>();
            var modelBinderFactory        = serviceProvider.GetRequiredService <IModelBinderFactory>();
            var modelMetadataProvider     = serviceProvider.GetRequiredService <IModelMetadataProvider>();
            actionSelector = new ODataActionSelector(
                actionsProvider,
                actionConstraintsProvider,
                loggerFactory,
                modelBinderFactory,
                modelMetadataProvider);
        }
        /// <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));
        }