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).
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
Post a Comment