.NetCore Odata Webapi sample

I uploaded one of my sample project to my new repo today.

There is a simple project to implement SalesOrders/SalesOrderLines with webapi. Also there is Odata implementation on it. Can be found here:


Simply, to your webapi add odata to your Nuget packages and implement Odata controllers

Call Odata with :


The result will be like :

using ARPredev2SO.Data;
using ARPredev2SO.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Routing;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ARPredev2SO.Controllers
    public class SalesOrdersOdataController : ODataController
        private readonly ApplicationDbContext _context;
        public SalesOrdersOdataController(ApplicationDbContext context)
            _context = context;
        // GET: api/Authors
        public IQueryable<SalesOrder> Get()
            return _context.SalesOrder.AsQueryable();

Add odata routing to Startup.cs and create your builder

  // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            app.UseMvc(routes =>
                routes.MapRoute(name: "default",template: "{controller=Home}/{action=Index}/{id?}");
                // Enable full OData queries, you might want to consider which would be actually enabled in production scenaries

                routes.MapODataServiceRoute("odata", "odata", GetEdmModel());

                // Work-around for #1175


        private static IEdmModel GetEdmModel()
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
                        .Filter() // Allow for the $filter Command
                        .Count() // Allow for the $count Command
                        .Expand() // Allow for the $expand Command
                        .OrderBy() // Allow for the $orderby Command
                        .Page() // Allow for the $top and $skip Commands
                        .Select();// Allow for the $select Command; 

            return builder.GetEdmModel();

Leave a Reply

Your email address will not be published. Required fields are marked *