Exemplo n.º 1
0
        public static ODataRoute CustomMapODataServiceRoute(this HttpConfiguration configuration, string routeName, string routePrefix)
        {
            ODataRoute route = configuration.MapODataServiceRoute(routeName, routePrefix, builder =>
            {
                // Get the model from the datasource of the current request: model-per-request.
                builder.AddService(ServiceLifetime.Scoped, sp =>
                {
                    IHttpRequestMessageProvider requestMessageProvider = sp.GetRequiredService <IHttpRequestMessageProvider>();
                    string dataSource = requestMessageProvider.Request.Properties[Constants.ODataDataSource] as string;
                    IEdmModel model   = DataSourceProvider.GetEdmModel(dataSource);
                    return(model);
                });

                // Create a request provider for every request. This is a workaround for the missing HttpContext of a self-hosted webapi.
                builder.AddService <IHttpRequestMessageProvider>(ServiceLifetime.Scoped, sp => new HttpRequestMessageProvider());

                // The routing conventions are registered as singleton.
                builder.AddService(ServiceLifetime.Singleton, sp =>
                {
                    IList <IODataRoutingConvention> routingConventions = ODataRoutingConventions.CreateDefault();
                    routingConventions.Insert(0, new MatchAllRoutingConvention());
                    return(routingConventions.ToList().AsEnumerable());
                });
            });

            CustomODataRoute odataRoute = new CustomODataRoute(route.RoutePrefix, new CustomODataPathRouteConstraint(routeName));

            configuration.Routes.Remove(routeName);
            configuration.Routes.Add(routeName, odataRoute);

            return(odataRoute);
        }
Exemplo n.º 2
0
        private static ODataRoute CustomMapODataServiceRoute(
            HttpRouteCollection routes,
            string routeName,
            string routePrefix,
            Func <HttpRequestMessage, IEdmModel> modelProvider,
            IODataPathHandler pathHandler,
            IEnumerable <IODataRoutingConvention> routingConventions,
            ODataBatchHandler batchHandler)
        {
            if (!string.IsNullOrEmpty(routePrefix))
            {
                int prefixLastIndex = routePrefix.Length - 1;
                if (routePrefix[prefixLastIndex] == '/')
                {
                    routePrefix = routePrefix.Substring(0, routePrefix.Length - 1);
                }
            }

            if (batchHandler != null)
            {
                batchHandler.ODataRouteName = routeName;
                string batchTemplate = string.IsNullOrEmpty(routePrefix)
                    ? ODataRouteConstants.Batch
                    : routePrefix + '/' + ODataRouteConstants.Batch;
                routes.MapHttpBatchRoute(routeName + "Batch", batchTemplate, batchHandler);
            }

            CustomODataPathRouteConstraint routeConstraint = new CustomODataPathRouteConstraint(
                pathHandler,
                modelProvider,
                routeName,
                routingConventions);
            CustomODataRoute odataRoute = new CustomODataRoute(routePrefix, routeConstraint);

            routes.Add(routeName, odataRoute);

            return(odataRoute);
        }
        private static ODataRoute CustomMapODataServiceRoute(
            HttpRouteCollection routes,
            string routeName,
            string routePrefix,
            Func<HttpRequestMessage, IEdmModel> modelProvider,
            IODataPathHandler pathHandler,
            IEnumerable<IODataRoutingConvention> routingConventions,
            ODataBatchHandler batchHandler)
        {
            if (!string.IsNullOrEmpty(routePrefix))
            {
                int prefixLastIndex = routePrefix.Length - 1;
                if (routePrefix[prefixLastIndex] == '/')
                {
                    routePrefix = routePrefix.Substring(0, routePrefix.Length - 1);
                }
            }

            if (batchHandler != null)
            {
                batchHandler.ODataRouteName = routeName;
                string batchTemplate = string.IsNullOrEmpty(routePrefix)
                    ? ODataRouteConstants.Batch
                    : routePrefix + '/' + ODataRouteConstants.Batch;
                routes.MapHttpBatchRoute(routeName + "Batch", batchTemplate, batchHandler);
            }

            CustomODataPathRouteConstraint routeConstraint = new CustomODataPathRouteConstraint(
                pathHandler,
                modelProvider,
                routeName,
                routingConventions);
            CustomODataRoute odataRoute = new CustomODataRoute(routePrefix, routeConstraint);
            routes.Add(routeName, odataRoute);

            return odataRoute;
        }