none
web api and oData configuration RRS feed

  • Question

  • Good morning

    I was trying to incorporate oData into a very simple API but cannot seem to get it to work.  I stripped this to the very bones based on a number of different examples

    Data Object

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    using System.ComponentModel.DataAnnotations;

    namespace oDataTemp.Models
    {
        public class Data
        {
            [Key]
            public int ID { get; set; }
            public string Description { get; set; }

            public Data() { }
        }
    }

    controller

                                

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;

    using System.Web.OData;
    using oDataTemp.Models;

    namespace oDataTemp.Controllers
    {

        [EnableQuery]
        public class TestController :ODataController
        {
            private List<Data> dataList;
            public TestController()
            {
                dataList = new List<Data>();

                for (int i = 0; i < 15; i++)
                {
                    dataList.Add(new Data() { ID = i, Description = string.Format("Name-{0}", i)});
                }



            }


            public HttpResponseMessage Get()
            {

                return Request.CreateResponse<IEnumerable<Data>>(HttpStatusCode.OK, dataList.AsQueryable());
            }


        }


    }

    and my WebapiConfig

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.OData.Edm;
    using System.Web.Http;
    using System.Web.OData.Batch;
    using System.Web.OData.Builder;
    using System.Web.OData.Extensions;

    using oDataTemp.Models;

    namespace oDataTemp
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services

                // Web API routes
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

                //config.MapODataServiceRoute("odata","api/", GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
                config.MapODataServiceRoute("odata", "odata", model: GetEdmModel());
                config.EnsureInitialized();

            }

            private static IEdmModel GetEdmModel()
            {
                ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
                builder.Namespace = "oDataTemp";
                builder.ContainerName = "DefaultContainer";
                builder.EntitySet<Data>("Data");
                var edmMOdel = builder.GetEdmModel();
                return edmMOdel;
            }
        }
    }

    However I keep getting the same error 

    <title>No non-OData HTTP route registered.</title>
            <meta name="viewport" content="width=device-width" />
            <style>
             body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 

    I didn't remove the ValuesController from the project and not matter what happens I get this message.  Any help would be greatly appreciated.

    Thank you.

    • Moved by CoolDadTx Wednesday, September 13, 2017 2:49 PM ASP.NET related
    Wednesday, September 6, 2017 3:33 PM

Answers

  • Hi Cheese Bread,

    Thank you for posting here.

    Please try the following code.

    Controller:

    namespace WebApiTest1.Controllers
    {
        [EnableQuery]
        public class TestController : ODataController
        {
            private List<Data> dataList;
            public TestController()
            {
                dataList = new List<Data>();
                for (int i = 0; i < 5; i++)
                {
                    dataList.Add(new Data() { ID = i, Description = string.Format("Name-{0}", i) });
                }
            }
            public HttpResponseMessage Get()
            {
                return Request.CreateResponse<IEnumerable<Data>>(HttpStatusCode.OK, dataList.AsQueryable());
            }
       }
    }
    

    Model:

        public class Data
        {
            [Key]
            public int ID { get; set; }
            public string Description { get; set; }
    
            public Data() { }
        
        }
    

    WebApiConfig:

    //OData V3
    namespace WebApiTest1
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                //oData3
               ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
                builder.EntitySet<Data>("Test");
                config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
            }
        }
    }
    

    Here is the result.

    If you use OData v4, you could refer to the following link.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

    WebApiConfig:

    //OData V4
    namespace WebApirTest2
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
               //clear all returning Formatters.
                config.Formatters.Clear();
                // set return json string
                config.Formatters.Add(new JsonMediaTypeFormatter());
                ODataModelBuilder builder = new ODataConventionModelBuilder();
                builder.EntitySet<Data>("Data");
                config.MapODataServiceRoute(
                    routeName: "ODataRoute",
                    routePrefix: null,
                    model: builder.GetEdmModel());
            }
        }
    }
    

    Here is the result.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Cheese Bread Thursday, September 21, 2017 1:42 PM
    Wednesday, September 13, 2017 7:44 AM

All replies

  • Hi Cheese Bread,

    Thank you for posting here.

    Please try the following code.

    Controller:

    namespace WebApiTest1.Controllers
    {
        [EnableQuery]
        public class TestController : ODataController
        {
            private List<Data> dataList;
            public TestController()
            {
                dataList = new List<Data>();
                for (int i = 0; i < 5; i++)
                {
                    dataList.Add(new Data() { ID = i, Description = string.Format("Name-{0}", i) });
                }
            }
            public HttpResponseMessage Get()
            {
                return Request.CreateResponse<IEnumerable<Data>>(HttpStatusCode.OK, dataList.AsQueryable());
            }
       }
    }
    

    Model:

        public class Data
        {
            [Key]
            public int ID { get; set; }
            public string Description { get; set; }
    
            public Data() { }
        
        }
    

    WebApiConfig:

    //OData V3
    namespace WebApiTest1
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                //oData3
               ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
                builder.EntitySet<Data>("Test");
                config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
            }
        }
    }
    

    Here is the result.

    If you use OData v4, you could refer to the following link.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-endpoint

    WebApiConfig:

    //OData V4
    namespace WebApirTest2
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
               //clear all returning Formatters.
                config.Formatters.Clear();
                // set return json string
                config.Formatters.Add(new JsonMediaTypeFormatter());
                ODataModelBuilder builder = new ODataConventionModelBuilder();
                builder.EntitySet<Data>("Data");
                config.MapODataServiceRoute(
                    routeName: "ODataRoute",
                    routePrefix: null,
                    model: builder.GetEdmModel());
            }
        }
    }
    

    Here is the result.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Cheese Bread Thursday, September 21, 2017 1:42 PM
    Wednesday, September 13, 2017 7:44 AM
  • you can post to the asp.net forum.

    http://forums.asp.net/

    Wednesday, September 13, 2017 9:48 AM