locked
Owin self host web-api with OData RRS feed

  • Question

  • User-26048972 posted

    I'm trying to create an owin self host webapi with odata support. I added all the dependencies and set everything up, when i call the get method (just by surfing to it with chrome). The breakpoint i put on the GET gets called and returns without an exception. But chrome just returns a blank screen, no http errors are found in the console of chrome. When i do this in IE10 (yes old version, but i am not allowed to update it) i get 406 (Not Acceptable).

    This is my Startup.cs code :

    public void Configuration(IAppBuilder appBuilder)
        {
            // Configure Web API for self-host. 
            HttpConfiguration config = new HttpConfiguration();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
    
            //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
            //config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
    
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Product>("Products");
    
            config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: null,
            model: builder.GetEdmModel());
    
            appBuilder.UseWebApi(config);
        }

    This is my controller code, just a simple GET.

     public class ProductsController : ODataController
    {
        ProductsContext db = new ProductsContext();
    
        [EnableQuery]
        public IQueryable<Product> Get()
        {
            try
            {
                return db.Products;
            }
            catch (System.Exception ex)
            {
                string x = "y";
                return db.Products;
            }
        }
    }

    db.Products is an empty table for now, but this should still return an empty array right ?

    Any help is greatly appreciated! Thanks!

    Wednesday, September 9, 2015 10:02 AM

Answers

  • User-84896714 posted

    Hi christophedm,

    I have tested your code, you could change code order and set "api" to routePrefix.

    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();
    
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Products");
        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: "api",
            model: builder.GetEdmModel());
    
    
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    
        appBuilder.UseWebApi(config); 
    }

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 10, 2015 5:55 AM

All replies

  • User-111650791 posted

    Hi Christo,

    Following link should help you.

    https://damienbod.wordpress.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/

    Thanks,

    Unnikrishnan S.N

    Wednesday, September 9, 2015 11:57 AM
  • User-84896714 posted

    Hi christophedm,

    I have tested your code, you could change code order and set "api" to routePrefix.

    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();
    
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Products");
        config.MapODataServiceRoute(
            routeName: "ODataRoute",
            routePrefix: "api",
            model: builder.GetEdmModel());
    
    
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    
        appBuilder.UseWebApi(config); 
    }

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 10, 2015 5:55 AM