Exemple #1
0
        public static void Register(HttpConfiguration config)
        {
            // OData V3 Route

            ODataV3.Builder.ODataModelBuilder builder3 = new ODataV3.Builder.ODataConventionModelBuilder();

            builder3.EntitySet <MarvelCharacter>("MarvelCharactersV3");
            // The MapODataRoute function is deprecated in WebAPI 2.2,
            // but I haven't found an alternative for supporting OData 3.
            config.Routes.MapODataRoute(
                routeName: "Marvel3",
                routePrefix: "dude3",
                model: builder3.GetEdmModel());

            // ODate V4 Route

            ODataV4.Builder.ODataModelBuilder builder4 = new ODataV4.Builder.ODataConventionModelBuilder();

            builder4.EntitySet <MarvelCharacter>("MarvelCharactersV4");
            ODataV4.Extensions.HttpConfigurationExtensions.MapODataServiceRoute(
                configuration: config,
                routeName: "Marvel4",
                routePrefix: "dude4",
                model: builder4.GetEdmModel());
        }
 internal static Microsoft.Data.Edm.IEdmModel GetEdmModel(this HttpActionDescriptor actionDescriptor, Type entityClrType)
 {
     if (actionDescriptor == null)
         throw new ArgumentNullException("actionDescriptor");
     if (entityClrType == (Type)null)
         throw new ArgumentNullException("entityClrType");
     else
         return actionDescriptor.Properties.GetOrAdd("MS_EdmModel" + entityClrType.FullName, _ =>
         {
             var builder = new System.Web.Http.OData.Builder.ODataConventionModelBuilder();
             var typeConfiguration = builder.AddEntity(entityClrType);
             builder.AddEntitySet(entityClrType.Name, typeConfiguration);
             return builder.GetEdmModel();
         }) as Microsoft.Data.Edm.IEdmModel;
 }
Exemple #3
0
        /// <summary>
        /// Setup OData http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx
        /// </summary>
        /// <param name="config"></param>
        private static void SetupODataRoutes(System.Web.Http.HttpConfiguration config)
        {
            var modelBuilder = new System.Web.Http.OData.Builder.ODataConventionModelBuilder();

            modelBuilder.EntitySet <Member>("Members");

            var model = modelBuilder.GetEdmModel();

            // Create the OData formatter and give it the model
            var odataFormatter = new System.Web.Http.OData.Formatter.ODataMediaTypeFormatter(model);

            // Register the OData formatter
            config.Formatters.Insert(0, odataFormatter);

            //Next you need to setup some routes to handle common OData requests, below are the routes required for a Read/Write OData model built using the OData Routing conventions that also supports client side code-generation (vital if you want a WCF DS client application to talk to your service).

            // Metadata routes to support $metadata and code generation in the WCF Data Service client.
            config.Routes.MapHttpRoute(
                System.Web.Http.OData.Builder.Conventions.ODataRouteNames.Metadata,
                "api/$metadata",
                new { Controller = "ODataMetadata", Action = "GetMetadata" }
                );
            config.Routes.MapHttpRoute(
                System.Web.Http.OData.Builder.Conventions.ODataRouteNames.ServiceDocument,
                "api",
                new { Controller = "ODataMetadata", Action = "GetServiceDocument" }
                );

            // Relationship routes (notice the parameters is {type}Id not id, this avoids colliding with GetById(id)).
            // This code handles requests like ~/ProductFamilies(1)/Products
            config.Routes.MapHttpRoute(System.Web.Http.OData.Builder.Conventions.ODataRouteNames.PropertyNavigation,
                                       "api/{controller}({parentId})/{navigationProperty}");

            // Route for manipulating links, the code allows people to create and delete relationships between entities
            config.Routes.MapHttpRoute(System.Web.Http.OData.Builder.Conventions.ODataRouteNames.Link,
                                       "api/{controller}({id})/$links/{navigationProperty}");

            // Routes for urls both producing and handling urls like ~/Product(1), ~/Products() and ~/Products
            config.Routes.MapHttpRoute(System.Web.Http.OData.Builder.Conventions.ODataRouteNames.GetById,
                                       "api/{controller}({id})");
            config.Routes.MapHttpRoute(
                System.Web.Http.OData.Builder.Conventions.ODataRouteNames.DefaultWithParentheses, "api/{controller}()");
            config.Routes.MapHttpRoute(System.Web.Http.OData.Builder.Conventions.ODataRouteNames.Default,
                                       "api/{controller}");
        }
        /// <summary>
        /// Setup OData http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx
        /// </summary>
        /// <param name="config"></param>
        private static void SetupODataRoutes(System.Web.Http.HttpConfiguration config)
        {
            var modelBuilder = new System.Web.Http.OData.Builder.ODataConventionModelBuilder();

            modelBuilder.EntitySet<Member>("Members");

            var model = modelBuilder.GetEdmModel();

            // Create the OData formatter and give it the model
            var odataFormatter = new System.Web.Http.OData.Formatter.ODataMediaTypeFormatter(model);

            // Register the OData formatter
            config.Formatters.Insert(0, odataFormatter);

            //Next you need to setup some routes to handle common OData requests, below are the routes required for a Read/Write OData model built using the OData Routing conventions that also supports client side code-generation (vital if you want a WCF DS client application to talk to your service).

            // Metadata routes to support $metadata and code generation in the WCF Data Service client.
            config.Routes.MapHttpRoute(
                System.Web.Http.OData.Builder.Conventions.ODataRouteNames.Metadata,
                "api/$metadata",
                new {Controller = "ODataMetadata", Action = "GetMetadata"}
                );
            config.Routes.MapHttpRoute(
                System.Web.Http.OData.Builder.Conventions.ODataRouteNames.ServiceDocument,
                "api",
                new {Controller = "ODataMetadata", Action = "GetServiceDocument"}
                );

            // Relationship routes (notice the parameters is {type}Id not id, this avoids colliding with GetById(id)).
            // This code handles requests like ~/ProductFamilies(1)/Products
            config.Routes.MapHttpRoute(System.Web.Http.OData.Builder.Conventions.ODataRouteNames.PropertyNavigation,
                                       "api/{controller}({parentId})/{navigationProperty}");

            // Route for manipulating links, the code allows people to create and delete relationships between entities
            config.Routes.MapHttpRoute(System.Web.Http.OData.Builder.Conventions.ODataRouteNames.Link,
                                       "api/{controller}({id})/$links/{navigationProperty}");

            // Routes for urls both producing and handling urls like ~/Product(1), ~/Products() and ~/Products
            config.Routes.MapHttpRoute(System.Web.Http.OData.Builder.Conventions.ODataRouteNames.GetById,
                                       "api/{controller}({id})");
            config.Routes.MapHttpRoute(
                System.Web.Http.OData.Builder.Conventions.ODataRouteNames.DefaultWithParentheses, "api/{controller}()");
            config.Routes.MapHttpRoute(System.Web.Http.OData.Builder.Conventions.ODataRouteNames.Default,
                                       "api/{controller}");
        }