ODATA WEB API(二)----ODATA服务与客户端
一、概述
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服务与客户端的更多相关文章
- ASP.NET Web API 2 OData v4教程
程序数据库格式标准化的开源数据协议 为了增强各种网页应用程序之间的数据兼容性,微软公司启动了一项旨在推广网页程序数据库格式标准化的开源数据协议(OData)计划,于此同时,他们还发 布了一款适用于OD ...
- [转]ASP.NET Web API对OData的支持
http://www.cnblogs.com/shanyou/archive/2013/06/11/3131583.html 在SOA的世界中,最重要的一个概念就是契约(contract).在云计算的 ...
- ASP.NET Web API基于OData的增删改查,以及处理实体间关系
本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先是比较典型的一对多关系,Supplier和Product. public class Product { ...
- [转]ASP.NET Web API基于OData的增删改查,以及处理实体间关系
本文转自:http://www.cnblogs.com/darrenji/p/4926334.html 本篇体验实现ASP.NET Web API基于OData的增删改查,以及处理实体间的关系. 首先 ...
- [转]ASP.NET web API 2 OData enhancements
本文转自:https://www.pluralsight.com/blog/tutorials/asp-net-web-api-2-odata-enhancements Along with the ...
- MVC项目实践,在三层架构下实现SportsStore-09,ASP.NET MVC调用ASP.NET Web API的查询服务
ASP.NET Web API和WCF都体现了REST软件架构风格.在REST中,把一切数据视为资源,所以也是一种面向资源的架构风格.所有的资源都可以通过URI来唯一标识,通过对资源的HTTP操作(G ...
- ODATA WEB API(一)---扩展使用
一.概述 时间也算充足,抽点时间总结下OData的常用的使用方式,开放数据协议(OData)是一个查询和更新数据的Web协议.OData应用了web技术如HTTP.Atom发布协议(AtomPub)和 ...
- 使用ASP.NET web API创建REST服务(二)
Creating a REST service using ASP.NET Web API A service that is created based upon the architecture ...
- [转]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- ...
随机推荐
- Android 本地加载网页与显示网络图片
有时候需要在应用程序里展示一些网页,但是需求里又明确指出,不允许打开系统浏览器,显然也不可能去编写一个浏览器出来,这时就需要使用 WebView控件,借助它我们就可以在自己的应用程序里嵌入一个浏览器, ...
- nginx+nginx-rtmp-module+ffmpeg搭建流媒体服务器[转]
转 :http://redstarofsleep.iteye.com/blog/2123752 Nginx本身是一个非常出色的HTTP服务器,FFMPEG是非常好的音视频解决方案.这两个东西通过一个n ...
- MySQL客户端Workbench
MySQL客户端除了Navicat之外,还有官方推出的MySQL Workbench,能看到数据库包含的存储过程,而Navicate不能. 下载链接: 32位:http://cdn.mysql.com ...
- OC block的简单使用
http://blog.csdn.net/itpeng523/article/details/23965147 一.先用Xcode创建一个空工程 学习block之前先用弄懂c语言的函数指针 看代码: ...
- Jquery ajax调用webservice总结
jquery ajax调用webservice(C#)要注意的几个事项: 1.web.config里需要配置2个地方 <httpHandlers> <remove verb ...
- Alien Dictionary
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 用C语言把双向链表中的两个结点交换位置,考虑各种边界问题。
用C语言把双向链表中的两个结点交换位置,考虑各种边界问题. [参考] http://blog.csdn.net/silangquan/article/details/18051675
- cocos2dx旧版本支持arm64修改
修改的版本是cocos2dx.2.2 1.在neon_matrix_impl.c中修改 #if defined(__ARM_NEON__)为 #if defined(_ARM_ARCH_7) 2.在m ...
- c++ template
在类中其中一个函数使用模板,函数定义和实现必须放在头文件里. ca.h template<typename T> void swap2(T &a,T &b) { T c=a ...
- ACM/ICPC 之 四道MST-Prim解法(POJ1258-POJ1751-POJ2349-POJ3026)
四道MST,适合Prim解法,也可以作为MST练习题. 题意包括在代码中. POJ1258-Agri Net 水题 //Prim-没什么好说的 //接受一个邻接矩阵,求MST //Time:0Ms M ...