Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register OData with ASP.NET 5

I have an ASP.NET 5 application and I would like to use OData v4 with it.

Here is what I have tried:

1.I imported the following nuget packages:

"Microsoft.AspNet.WebApi": "5.2.3",
"Microsoft.AspNet.OData": "5.7.0",
"Microsoft.AspNet.Hosting": "1.0.0-rc1-final"

2.Called this in the Startup.Configure method

GlobalConfiguration.Configure(ConfigOData);

3.And finally this is the OData configuration

private static void ConfigOData(HttpConfiguration config)
{
    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

    var EDM = builder.GetEdmModel();

    //OData v4.0
    config.MapODataServiceRoute("odata", "odata", EDM,
        new DefaultODataPathHandler(),
        conventions,
        new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
}

Now the OData calls are being processed by the MVC's routing configuration (most probably because I did not register OData with ASP.NET 5 properly).

Can someone help me with this please ?

like image 930
Ayman Avatar asked Nov 29 '15 18:11

Ayman


1 Answers

Here is how we can configure it with the ASP.NET Core RC2 OData.

namespace ODataSample
{
    using Microsoft.AspNetCore.OData.Extensions;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.Extensions.DependencyInjection;
    using ODataSample.Models;

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddOData<ISampleService>();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseOData("odata");
            app.UseMvc();
        }
    }
}

Here is how you can try it yourself. You will need to have the .NET Core SDK installed.

git clone [email protected]:bigfont/WebApi.git

cd WebApi\vNext\src\Microsoft.AspNetCore.OData
dotnet restore

cd ..\..\samples\ODataSample.BigFont\
dotnet restore
dotnet run

This is the result at http://localhost:5000/odata

Result

Links

  • The repository in GitHub.
  • The ISampleService in GitHub.
like image 196
Shaun Luttin Avatar answered Oct 03 '22 19:10

Shaun Luttin