本文转自:http://mahedee.net/tag/web-api/
What is OData?
OData Stands for Open Data Protocol. It 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). OData consumption can be done across different Programming Language. ASP.NET Web API supports both OData v3 and V4.
Advantages of OData Services
- OData based on REST Architecture so we can retrieve data using URL
- Support CRUD Operation using HTTP Method like GET, POST, PUT, DELETE
- Support HTTP, JSON and Atom Pub
- It is very light weight, so interaction of client and server is very fast
Disadvantage
- Since it is URL based so many people think it is less secure
- It does not support all type of custom query
Let’s implement OData Services using ASP.NET Web API
Tools and Technology used
I used following tools and technology to develop the project –
- Visual Studio 2013
- Visual C#
- ASP.NET Web API 2
- Entity Framework 6
- Postman(Google postman)
Step 1: Create a ASP.net Web API Project
Open visual studio and then go
File -> Project -> ASP.NET Web Application
Now select Web API and press OK
Step 2: Install Microsoft.AspNet.Odata
To install OData Nuget Pacakge from Package Manager Console.
Select Tool -> NuGet Package Manager > Package Manager Console
Type following command in package manager console
PM> Install-Package Microsoft.AspNet.Odata
Step 3: Create a model name Employee
Create a Model name Employee in model folder
1 2 3 4 5 6 7 8 |
|
Step 4: Change or Add Connection String
Change or Add connection string in Web.config
1 2 |
|
Step 5: Create a Context class
1 2 3 4 5 6 7 8 9 10 |
|
Step 6: Add a Controller
Press right button on Controller folder -> Add -> Controller
Now choose “Web API 2 OData v3 Controller with actions, using Entity Framework” scaffolding template and then press Add.
Now choose Controller Name as EmployeeController, Model name as Employee and Context name as HRMContext and click Add like below.
The following code will be generated on corresponding for the controller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
Step 7: Configure OData End Point
Open the file App_Start/WebApiConfig.cs. Add the following using statements:
using System.Web.Http.OData.Builder;
using System.Web.Http.OData.Extensions;
using Web.OData.Models;
Add the following code in the register method.
1 2 3 4 5 6 7 8 9 10 |
|
Step 8: Enable Migration
Type the following command in package manager console to enable migration
PM> Enable-Migrations -ContextTypeName HRMContext
After pressing enter you will see a class name Configuration is created in Mingrations folder with some codes.
Step 9: Add seed data and add migration
Modify the Seed() method of Configuration class like below to add some seed data.
1 2 3 4 5 6 7 8 9 10 11 |
|
Now type the following command in the package manager console to add a migration.
PM> Add-Migration initialmigration
Step 10: Update database and attaché mdf file
Now type the following command in package manager console.
PM> Update-Database –Verbose
You will see two file .mdf and .ldf is created in your App_data directory. Now attached the file like below.
Now run you application. Run Postman. Type http://localhost:64126/odata/Employee in your postbox you will see following output in JSON format. Use port number on which your application currently running instead of 64126.
Now, it’s working…!! Cheers!!!