0
votes

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.

1
Why you do twice route config, the first one isn't necessary, you can refer to samples here github.com/OData/ODataSamples/tree/master/WebApi - Fan Ouyang
Well i thought the OData layer was going to magically be put on top of the original web api :-). - Christophe

1 Answers

0
votes

Thanks Fan Ouyang for the link, this has helped me resolve my issue! Now i'm using the following code :

Owin Startup

        var config = new HttpConfiguration();
        config.MapODataServiceRoute(routeName: "OData", routePrefix: "odata", model: GetEdmModel());
        appBuilder.UseWebApi(config);

GetEdmModel Function

    private IEdmModel GetEdmModel()
    {
        var modelBuilder = new ODataConventionModelBuilder();
        modelBuilder.EntitySet<Customer>("Customers");
        return modelBuilder.GetEdmModel();
    }

Controller GET

   public PageResult<Customer> Get(ODataQueryOptions<Customer> queryOptions)
    {
        IQueryable results = queryOptions.ApplyTo(CustomerList.AsQueryable());
        return new PageResult<Customer>(results as IEnumerable<Customer>, Request.ODataProperties().NextLink, CustomerList.Count);
    }