Skip to main content

How to create OData service by using ASP.Net Web API 2 in MVC application step by step


Title :  How to create OData service  by using ASP.Net Web API 2 in MVC application

Prerequisites:  Visual Studio 2015 or any version higher  & SQL Server 2008 or above.

Let's get started

What’s OData protocol?

The Open Data Protocol (OData) is a data access protocol for the web. OData provides a uniform way to query and manipulate data sets through CRUD operations (create, read, update, and delete).

Official Site :  http://www.odata.org/

Step 1 : Open Visual Studio and select File >> New Project

The "New Project" window will pop up. Select ASP.NET Web Application (.NET Framework), name your project, and click OK.



   Next Step Select Web API Template and select Ok
  


   
   Project Template is created as shown below


  Step 3 : Download Northwind and pubs Sample Databases for SQL Server 2000    using the link and restore it.




 Step 4 : Add new Folder DbContext to our project 

\
Add New Item  : ADO .NET Entity Model
   



   
  Select Add


   Press Next and Hit New Connection


   Provide SQL details and Press Test connection



  Select Ok



       Select the entities as per requirement



   Press Finish and wait till the classes are generating


   Build the solution




  Create a Controller 

  Now, we are going to create a Controller. Right click on the Controllers folder > Add >     Controller> selecting Web API 2 OData v3 Controller with actions, using Entity Framework >    click Add.



Next step is - In Solution Explorer, select App_Start > double click on WebApiConfig.cs, then we should add the following code to the register method:


Add Namespace at top of the file


using System.Web.Http.OData.Builder;
using SampleODataService.DbContext;

        // Add following block to register method
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<Product>("Products");
            config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());


   Now build the solution and run the application using F5 and append "/odata"       in the url and press enter

   It will list our all entities that we are added in register method





   OData service is created now we can use it anywhere we want. 

Comments

Popular posts from this blog

Create Openui5 sample application from scratch using Visual Studio 2013

Title :   Create Openui5 sample application setup from scratch using Visual Studio 2013 step by step Introduction :  Hello everyone, in this tutorial we will learn how to create project in openui5 using visual studio 2013, the complete project folder setup step by step. So let's get started. Prerequisites :  Visual Studio 2013 or any version higher than Visual Studio 2010, I am using 2013 version Note : I am assuming that you have prior knowledge about openui5 technology, component,manifest and mvc pattern that is used in this application (basics of javascript ,html and css,json and xml and visual studio) & make sure internet is connected because sap code library cdn is included. Step 1 : Open Visual Studio and select File >> New Project. The "New Project" window will pop up. Select ASP.NET Empty Web Application (.NET Framework), name your project, and click OK. Your blank project is created  then Right click on proje...