一、概述

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. nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器[转]

    转 :http://redstarofsleep.iteye.com/blog/2123752 Nginx本身是一个非常出色的HTTP服务器,FFMPEG是非常好的音视频解决方案.这两个东西通过一个n ...

  2. 获取客户端IP

    function getIP(){ $ip = ""; if (getenv("HTTP_CLIENT_IP") && strcasecmp(g ...

  3. EXTJS 6 必填项加星号*

    /**重写ext filed组件, 实现表单必填项加红色*星号**/ Ext.override(Ext.form.field.Base,{ initComponent:function(){ if(t ...

  4. dtw算法

                              dtw路径与线性变换路径对比 转自:http://baike.baidu.com/link?url=z4gFUEplOyqpgboea6My0mZP ...

  5. 【架构】Google的大规模集群管理工具Borg

    参考资料: http://www.cnblogs.com/YaoDD/p/5374393.html http://www.cnblogs.com/YaoDD/p/5351589.html

  6. git cherry-pick简介

    本文编辑整理自: http://sg552.iteye.com/blog/1300713 http://web.mit.edu/bitbucket/git-doc/git-cherry-pick.tx ...

  7. jQuery获取循环中的选中单选按钮radio的值

    1.<input type="radio" name="testradio" value="jquery获取radio的值" /> ...

  8. centos7时间同步和时区设置

    centos7时间同步和时区设置 安装ntp服务的软件包 sudo yum install ntp 将ntp服务设置为缺省启动 systemctl enable ntpd 修改启动参数,增加-g -x ...

  9. std::map

    1.例: map<int,string> m_mapTest; m_mapTest.insert(make_pair(1,"kong")); m_mapTest.ins ...

  10. [转][Android]FragmentPagerAdapter与FragmentStatePagerAdapter使用详解与区别

    原文链接:http://blog.csdn.net/zhaokaiqiang1992 FragmentPagerAdapter是android-support-v4支持包里面出现的一个新的适配器,继承 ...