本文转自:http://www.c-sharpcorner.com/UploadFile/dacca2/work-with-odata-in-web-api-create-your-first-odata-service/?utm_source=tuicool&utm_medium=referral

This is the “Work with Odata in Web API” article series. This article of the series explains various parts of Odata service in context of the ASP.NET Web API. The previous article provided an introduction to Odata. You can read it here:
Work With Odata in Web API : Introduction of ASP.NET Odata service
In this article we will create our first Odata service in the ASP.NET Web API 2 environment. It's a highly practical example of a simple Odata service. I will assume you have a basic understanding of the MVC architecture and Web API service.
Ok, if you still want to continue then that implies you have the required skills and want to learn Odata services in the Microsoft platform. Fine, use the following procedure and you will get it done.
Create one MVC 4 project in Visual Studio Don't forget to select the MVC 4 template otherwise we will not get a template of the Web API. If you are using Visual Studio 2010 then you need to install the template externally.

Select Web API project Make sure you have selected a Web API project and the view engine part is not necessary and not important in our example, so leave it as it is.

Once you click on OK, the project will be created in the Visual Studio environment and the next step is to install a few packages from the Nugget Package Manager.
Install Web API 2 package In my case I opened the Web API version 1.0 application. To support Odata, we need to upgrade the Web API version. So, go to the Nuget Package Manager and search for the Web API 2.0 package.

Once you press install you will see the package being installed and in the middle of the installation you might encounter the following screen. It's asking you to accept the terms and condition of dependent packages. Just click “Accept” and proceed.

Once we finish our Web API up gradation, we will install the Odata service package from the same package manager. Search for “odata service package” and install the following package in the application.

Again in the middle of the application you will encounter the following screen that will ask to accept the license agreement. Click “Accept” and proceed.


Fine, we have set up our packages and all installation has finished. Now, we can start to write code to expose our Odata service.
Create Model class At first we will create a Model class that we will expose as a data model in the Odata service. In this example we created a “person” model class as in the following. It has four properties.

Add Odata controller If you are experienced with MVC then you re familiar with the MVC controller but in an Odata service the controller is the Odata controller that we will add to the application shortly.
So, right-click on the controller folder and click on the menu as in the following screen.

It will open the following window. At first we need to provide a controller name and then model name. In the data context box just click on the "Add data context" button and provide the name as in the following screen.

Configure Odata route This is a very important part of Odata service configuration. Open the “WebApiConfgi.cs” file from the App_Start folder. We need to register the route of our Odata servce.

public static void Register(HttpConfiguration config)

{

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

builder.EntitySet<Models.personModel>("Person");

config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());

config.EnableSystemDiagnosticsTracing();

}

Implement Get() method in controller Here we have implemented a simple controller that will expose the service. In the Get() method we have just hard-coded some data. In reality obviously you will use a DB to fetch the data.

using System;

using System.Collections.Generic;

using System.Data;

using System.Data.Entity;

using System.Data.Entity.Infrastructure;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using System.Web.Http.ModelBinding;

using System.Web.Http.OData;

using System.Web.Http.OData.Routing;

using MvcApplication2.Models;

namespace MvcApplication2.Controllers

{

public class personController : ODataController

{

[Queryable]

public IQueryable<personModel> Getperson()

{

List<personModel> list = new List<personModel>{

new personModel{Id=1, name = "Sourav",surname="kayal",age=26},

new personModel{Id=2, name = "Ram",surname="Das",age=26}

};

return list.AsQueryable<personModel>();

}

}

}

Fine, our Odata service is now ready to be exposed. Let's run the application and try to consume the Odata service. Open a browser window and point to the Get() action in the person controller.
Oh, Cheers; our first Odata service is running successfully. We are getting a list of all persons.
Now, if we want to filter the results with a query, something like this:
Have a look at the URL part. In the URL we are passing a query to get the information with the Id 1. Anyway we will explain the query in a later article.
Conclusion This article implemented an Odata service practically. I hope you have understood it and successfully implemented it. In a future article we will understand more about Odata services.

 

[转]Work With Odata in Web API: Create Your First Odata Service的更多相关文章

  1. 创建基于OData的Web API - Knowledge Builder API, Part IV: Write Controller

    基于上一篇<创建基于OData的Web API - Knowledge Builder API, Part III:Write Model and Controller>,新创建的ODat ...

  2. 创建基于OData的Web API - Knowledge Builder API, Part I:Business Scenario

    在.NET Core 刚刚1.0 RC的时候,我就给OData团队创建过Issue让他们支持ASP.NET Core,然而没有任何有意义的答复. Roadmap for ASP.NET Core 1. ...

  3. Web Api系列教程第2季(OData篇)(二)——使用Web Api创建只读的OData服务

    前言 很久没更新了,之前有很多事情,所以拖了很久,非常抱歉.好了,废话不多说,下面开始正题.本篇仍然使用上一季的的项目背景(系列地址http://www.cnblogs.com/fzrain/p/34 ...

  4. [转]Web Api系列教程第2季(OData篇)(二)——使用Web Api创建只读的OData服务

    本文转自:http://www.cnblogs.com/fzrain/p/3923727.html 前言 很久没更新了,之前有很多事情,所以拖了很久,非常抱歉.好了,废话不多说,下面开始正题.本篇仍然 ...

  5. Create an OData v4 Endpoint Using ASP.NET Web API 2.2(使用ASP.NET Web API 2.2创建OData v4端点)

    开放数据协议Open Data Protocol(OData)是web的一种数据存取协议,OData通过设置CRUD操作(Create创建.Read读取.Update更新,Delete删除)提供一种统 ...

  6. ODATA WEB API(二)----ODATA服务与客户端

    一.概述 ODATA不经可以作为WebAPI建立相应的WEBAPI控制器,还可以建立ODataControl控制器,能够通过插件建立第三方ODataClinet类库:调用和使用数据变得简单可行. 二. ...

  7. 创建基于OData的Web API - Knowledge Builder API, Part II:Project Setup

    本篇为Part II:Project Setup 查看第一篇<Part I:  Business Scenario> 第一步,准备步骤. 准备步骤一,下载.NET Core 2.2 SDK ...

  8. web api 2 学习笔记 (OData Batch request)

    之前介绍过OData 中实现RPC的写法,今天在来一个批量操作. 参考 : https://damienbod.wordpress.com/2014/08/14/web-api-odata-v4-ba ...

  9. web api 2 学习笔记 (Odata ODataQueryOptions 使用)

    [ODataRoutePrefix("products")] public class ProductController : BaseController { [ODataRou ...

随机推荐

  1. SCVMM 安装

    1.所有的物理机必须在域环境下 2.安装VMM和SQL使用的域账号尽量相同,并且唯一,尽量与管理hyper v主机使用的账号不相同

  2. 《Beginning Java 7》 - 3 - Equalty 判等

    == 用于判断是否为同一引用. 比如对于 String: System.out.println("abc" == "abc"); // Output: true ...

  3. PLSQL Developer 13.0.0.1883 注册码

    PLSQL Developer 13.0.0.1883 注册码 product code: 4vkjwhfeh3ufnqnmpr9brvcuyujrx3n3le serial Number:22695 ...

  4. bzoj 3669: [Noi2014]魔法森林(并查集+LCT)

    Description 为了得到书法大家的真传,小E同学下定决心去拜访住在魔法森林中的隐士.魔法森林可以被看成一个包含个N节点M条边的无向图,节点标号为1..N,边标号为1..M.初始时小E同学在号节 ...

  5. secureCRT颜色方案设置

    按照如下设置后vim编辑会有如下颜色提示

  6. 【spring】Spring Boot:定制自己的starter

    概念 在学习Spring Boot的过程中,接触最多的就是starter.可以认为starter是一种服务——使得使用某个功能的开发者不需要关注各种依赖库的处理,不需要具体的配置信息,由Spring ...

  7. PyQt5(3)——信号和槽初探

    信号和槽是QT的核心机制(当信号发射时,链接的槽函数会自动执行) 在pyqt5中信号和槽通过 QObject.signal.connect()链接. QObject类或子类(QWidget)派生的类都 ...

  8. 【数据结构】10分钟教你用栈求解迷宫老鼠问题超详细教程附C++源代码

    问题描述 给定一张迷宫地图和一个迷宫入口,然后进入迷宫探索找到一个出口.如下图所示: 该图是一个矩形区域,有一个入口和出口.迷宫内部包含不能穿越的墙壁或者障碍物.这些障碍物沿着行和列放置,与迷宫的边界 ...

  9. js的事件机制二

    js的事件机制二 1.给合适的HTML标签添加合适的事件 onchange-----select下拉框 onload-----body标签 单双击-----用户会进行点击动作的HTML元素 鼠标事件 ...

  10. 网络传输层之TCP/UDP详解

    一.运输层协议概述 从通信和信息处理的角度看,运输层向它上面的应用层提供通信服务,它属于面向通信部分的最高层,同时也是用户功能中的最低层. 运输层的任务就是负责主机中两个进程之间的通信,其数据传输的单 ...