.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:

https://dev.azure.com/arbistechdev/APredev2SO%20NetCore%20Razor%20SPA

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

Call Odata with :

http://localhost:58817/odata/salesorder

The result will be like :

{"@odata.context":"http://localhost:58817/odata/$metadata#SalesOrder","value":[]}
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
{
    [ODataRoutePrefix("SalesOrder")]
    public class SalesOrdersOdataController : ODataController
    {
        private readonly ApplicationDbContext _context;
        public SalesOrdersOdataController(ApplicationDbContext context)
        {
            _context = context;
        }
        // GET: api/Authors
        [EnableQuery]
        [HttpGet]
        [ODataRoute]
        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)
        {
           ...
            services.AddOData();
 ...
        }

        // 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.Count().Filter().OrderBy().Expand().Select().MaxTop(null);


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

                // Work-around for #1175
                routes.EnableDependencyInjection();
            });

        }

        private static IEdmModel GetEdmModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Product>("Product")
                        .EntityType
                        .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; 
            builder.EntitySet<SalesOrder>("SalesOrder");
            builder.EntitySet<SalesOrderLine>("SalesOrderLine");

            return builder.GetEdmModel();
        }

Leave a Reply

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