一、概述

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

二、建立OData Web API 服务端

1、通过NuGet添加第三方Microsoft.AspNet.OData;

2、建立相应的Model:Person和Trip

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; namespace MyMVCODataTwo.Models
{
public class Person
{
[Key]
public String ID { get; set; }
[Required]
public String Name { get; set; }
public String Description { get; set; }
public List<Trip> Trips { get; set; }
}
} using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace MyMVCODataTwo.Models
{
public class Trip
{
[Key]
public String ID { get; set; }
[Required]
public String Name { get; set; }
}
}

3、添加程序集合,代替数据库请求:

using MyMVCODataTwo.Models;
using System.Collections.Generic;
namespace MyMVCODataTwo.DataSource
{
public class DemoDataSources
{
private static DemoDataSources instance = null;
public static DemoDataSources Instance
{
get
{
if (instance == null)
{
instance = new DemoDataSources();
}
return instance;
}
}
public List<Person> People { get; set; }
public List<Trip> Trips { get; set; }
private DemoDataSources()
{
this.Reset();
this.Initialize();
}
public void Reset()
{
this.People = new List<Person>();
this.Trips = new List<Trip>();
}
public void Initialize()
{
this.Trips.AddRange(new List<Trip>()
{
new Trip()
{
ID = "",
Name = "Trip 0"
},
new Trip()
{
ID = "",
Name = "Trip 1"
},
new Trip()
{
ID = "",
Name = "Trip 2"
},
new Trip()
{
ID = "",
Name = "Trip 3"
}
});
this.People.AddRange(new List<Person>
{
new Person()
{
ID = "",
Name = "Angel",
Trips = new List<Trip>{Trips[], Trips[]}
},
new Person()
{
ID = "",
Name = "Clyde",
Description = "Contrary to popular belief, Lorem Ipsum is not simply random text.",
Trips = new List<Trip>{Trips[], Trips[]}
},
new Person()
{
ID = "",
Name = "Elaine",
Description = "It has roots in a piece of classical Latin literature from 45 BC, making Lorems over 2000 years old."
}
});
}
}
}

4、WebApi配置:

public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapODataServiceRoute("odata", null, GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.EnsureInitialized();
}
private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = "Demos";
builder.ContainerName = "DefaultContainer";
builder.EntitySet<Person>("People");
builder.EntitySet<Trip>("Trips");
var edmModel = builder.GetEdmModel();
return edmModel;
}
}

参考博客:

http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/

三、建立独立的控制台客户端:

1、通过扩展工具管理,下载OData Client Code ;

2、添加控制台引用程序,添加OData Client Item;

修改MetadataDocumentUri 地址: public const string MetadataDocumentUri = "http://localhost:8090";

3、编写应用端代码:

 static void Main(string[] args)
{
// TODO: Replace with your local URI.
string serviceUri = "http://localhost:8090/";
var container = new DefaultContainer(new Uri(serviceUri)); foreach (var p in container.People)
{
Console.WriteLine("{0} {1} {2}", p.ID, p.Name, p.Description);
} Console.Read();
}

参看博客:

http://www.cnblogs.com/bluedoctor/p/4384659.html

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/create-an-odata-v4-client-app

源码下载(包括服务端、客户端、以及OData客户端封装类库)

ODATA WEB API(二)----ODATA服务与客户端的更多相关文章

  1. ASP.NET Web API 2 OData v4教程

    程序数据库格式标准化的开源数据协议 为了增强各种网页应用程序之间的数据兼容性,微软公司启动了一项旨在推广网页程序数据库格式标准化的开源数据协议(OData)计划,于此同时,他们还发 布了一款适用于OD ...

  2. [转]ASP.NET Web API对OData的支持

    http://www.cnblogs.com/shanyou/archive/2013/06/11/3131583.html 在SOA的世界中,最重要的一个概念就是契约(contract).在云计算的 ...

  3. ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先是比较典型的一对多关系,Supplier和Product. public class Product { ...

  4. [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系

    本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...

  5. [转]ASP.NET web API 2 OData enhancements

    本文转自:https://www.pluralsight.com/blog/tutorials/asp-net-web-api-2-odata-enhancements Along with the ...

  6. MVC项目实践,在三层架构下实现SportsStore-09,ASP.NET MVC调用ASP.NET Web API的查询服务

    ASP.NET Web API和WCF都体现了REST软件架构风格.在REST中,把一切数据视为资源,所以也是一种面向资源的架构风格.所有的资源都可以通过URI来唯一标识,通过对资源的HTTP操作(G ...

  7. ODATA WEB API(一)---扩展使用

    一.概述 时间也算充足,抽点时间总结下OData的常用的使用方式,开放数据协议(OData)是一个查询和更新数据的Web协议.OData应用了web技术如HTTP.Atom发布协议(AtomPub)和 ...

  8. 使用ASP.NET web API创建REST服务(二)

    Creating a REST service using ASP.NET Web API A service that is created based upon the architecture ...

  9. [转]Using $select, $expand, and $value in ASP.NET Web API 2 OData

    本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/using- ...

随机推荐

  1. Hello 2016

    Hello 2016 I am really happy to work and study here. Nothing is better than be oneself ! It's import ...

  2. 仿QQ侧滑菜单<大自然的搬运工-代码不是我的>

    1.记录下效果图 2.二个工具类 package myapplication.com.myapplicationfortest.utils; import android.util.Log; /** ...

  3. FIDO 标准简介

    FIDO 联盟(Fast IDentity Online Alliance)简介 网站:http://fidoalliance.org FIDO Alliance,成立于2012年7月. FIDO的目 ...

  4. codecademy-command line_filesystem

    $:shell prompt (命令提示符) In the terminal, first you see $. This is called a shell prompt. It appears w ...

  5. nyoj123_士兵杀敌(四)_树状数组_插线求点

    士兵杀敌(四) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战 ...

  6. VC++ 模块与资源分离

    在一些开发过程中,需要模块支持中英文语言切换,比较好的实现方式是从模块中将资源分离出来,做成中英文两个资源dll,根据需要加载不同的dll从而实现切换不同的语言显示. 新建一个资源dll文件,选择Wi ...

  7. Android Canvas绘图详解(图文)

    编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅是Android知识.前端.后端以至于产品和设计都有涉猎,想成为全栈工程师的朋友不要错过! Andr ...

  8. QL Server 实用脚本

    use MyFirstDB; -- 主要内容 -- SQL Server 实用脚本 -- 1.case语句 -- 2.子查询 -- 3.连接查询 -- 4.脚本变量与流程控制(选择与循环等) -- 5 ...

  9. IOS- 快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序

    /*******************************快速排序 start**********************************///随即取 当前取第一个,首先找到第一个的位置 ...

  10. UI课堂笔记

    2016.7.18 + (UIColor *)blackColor; + (UIColor *)darkGrayColor;   深灰色 + (UIColor *)lightGrayColor;  浅 ...