[转]How to Use Web API OData to Build an OData V4 Service without Entity Framework
Mar 12, 2015 • Qian Li
There are quite a lot of tutorials showing how to create OData services using Web API OData, but these requires Entity Framework and a database server behind. If you want a quick try or you have your own way of implementing data sources, these tutorials may not be the best fit. In this article, we will show how to build an OData service using in-memory data as data source and with basic function.
Create the solution
Create a new solution following File -> New -> Project -> Web, then choose ASP.NET Web Application
. Name it with Demo
. Then in the upcoming dialogue box, choose Empty
and check Web API
, click OK.
Install NuGet packages
Run the following command in the Package Manager Console.
PM> Install-Package Microsoft.AspNet.OData
Add Models
In this getting-started example, we just add two model class Person.cs
and Trip.cs
under folder Models
. Person
can navigate to Trips
.
using System;
using System.ComponentModel.DataAnnotations;
namespace Demo.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 Demo.Models
{
public class Trip
{
[Key]
public String ID { get; set; }
[Required]
public String Name { get; set; }
}
}
The attributes [Key]
and [Required]
are all from System.ComponentModel.DataAnnotations
meaning the property is key and required seperately.
In-Memory data source
This tutorial uses in-memory data source, which is more flexible. Below are only one way to implement, you can definitely have your only way.
Add a folder DataSource
and add a class file DemoDataSources.cs
with the code below.
using Demo.Models;
using System.Collections.Generic;
namespace Demo.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 = "0",
Name = "Trip 0"
},
new Trip()
{
ID = "1",
Name = "Trip 1"
},
new Trip()
{
ID = "2",
Name = "Trip 2"
},
new Trip()
{
ID = "3",
Name = "Trip 3"
}
});
this.People.AddRange(new List<Person>
{
new Person()
{
ID = "001",
Name = "Angel",
Trips = new List<Trip>{Trips[0], Trips[1]}
},
new Person()
{
ID = "002",
Name = "Clyde",
Description = "Contrary to popular belief, Lorem Ipsum is not simply random text.",
Trips = new List<Trip>{Trips[2], Trips[3]}
},
new Person()
{
ID = "003",
Name = "Elaine",
Description = "It has roots in a piece of classical Latin literature from 45 BC, making Lorems over 2000 years old."
}
});
}
}
}
Add Controllers
Since there are two entity set, we will add two controller class under the folder Controllers
First is controller for People
using Demo.DataSource;
using System.Linq;
using System.Web.Http;
using System.Web.OData;
namespace Demo.Controllers
{
[EnableQuery]
public class PeopleController : ODataController
{
public IHttpActionResult Get()
{
return Ok(DemoDataSources.Instance.People.AsQueryable());
}
}
}
Then is the controller for Trips
using Demo.DataSource;
using System.Linq;
using System.Web.Http;
using System.Web.OData;
namespace Demo.Controllers
{
[EnableQuery]
public class TripsController : ODataController
{
public IHttpActionResult Get()
{
return Ok(DemoDataSources.Instance.Trips.AsQueryable());
}
}
}
In this very simple implementation, only simple Get
with query options are allowed. If you want to enable more capabilities in your controller, the code is quite similar with what's done with EF as data source. Please refer to ASP.NET Web API OData V4 Samples and Create an OData v4 Endpoint Using ASP.NET Web API 2.2.
Configure the Endpoint
The last step is to modify the WebApiConfig.cs
file under App_Start
.
using Demo.Models;
using Microsoft.OData.Edm;
using System.Web.Http;
using System.Web.OData.Batch;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
namespace Demo
{
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;
}
}
}
Try with it
It's done to create a very basic OData endpoint with in-memory data source using Web API OData. You can try it out now! All samples below are all GET
method.
Service document
http://localhost:[portNumber]/
Service metadata
http://localhost:[portNumber]/$metadata
Get People
http://localhost:[portNumber]/People
Queries
http://localhost:[portNumber]/People?$filter=contains(Description,'Lorem')
http://localhost:[portNumber]/People?$select=Name
http://localhost:[portNumber]/People?$expand=Trips
[转]How to Use Web API OData to Build an OData V4 Service without Entity Framework的更多相关文章
- 【ASP.NET Web API教程】2.3 与实体框架一起使用Web API
原文:[ASP.NET Web API教程]2.3 与实体框架一起使用Web API 2.3 Using Web API with Entity Framework 2.3 与实体框架一起使用Web ...
- asp.net web api 2.2 基础框架(带例子)
链接:https://github.com/solenovex/asp.net-web-api-2.2-starter-template 简介 这个是我自己编写的asp.net web api 2.2 ...
- [转]Creating an OData v3 Endpoint with Web API 2
本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata- ...
- [转]Work With Odata in Web API: Create Your First Odata Service
本文转自:http://www.c-sharpcorner.com/UploadFile/dacca2/work-with-odata-in-web-api-create-your-first-oda ...
- Web Api 简介
ASP.NET Web API 简介 ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP. ...
- ASP.NET Web API 简介
ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...
- [水煮 ASP.NET Web API 2 方法论] 目 录
一.ASP.NET 中的 Web API [水煮 ASP.NET Web API2 方法论](1-1)在MVC 应用程序中添加 ASP.NET Web API 与 ASP.NET MVC 在同一个进程 ...
- ASP.NET Web API 特性
ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...
- Asp.net web api 知多少
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...
随机推荐
- 简单几步,提升.Net Core的开发效率
附加IIS进程调式? 以前在开发ASP.NET(MVC)项目的时候,为了加快程序的启动速度(调式),我们会选择使用IIS.先用IIS架设还在开发的项目,在需要调式的时候附加进程,而在更多时候,如果调整 ...
- 201621123012《Java程序设计》第13次学习总结
作业 - 13 网络 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能(购物车.图书馆管理.斗地主等)-分组完成 为了让你的系 ...
- 贪心——Prim算法(避圈法)
1.简介 Prim算法是图论中的一种算法,可在带权连通图里搜索产生最小生成树. 该算法于1930年由捷克数学家沃伊捷赫·亚尔尼克(Vojtěch Jarník)发现:并在1957年由美国计算机科学家罗 ...
- secureCRT颜色方案设置
按照如下设置后vim编辑会有如下颜色提示
- Archlinux 下系统如何设置让 Wine 调用 ibus输入法
前言: 如果你是fcitx输入法用户,那么这篇文章大可不必看.fcitx是一个非常强大的框架,著名搜狗输入法就是基于fcitx输入法架构开发的.据我所知.您遇到这个问题可以通过卸载ibus输入法进行修 ...
- Corn表达式
CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...
- 在iOS7中修改键盘Return键的类型
今天将之前运行在iOS7之前的一段代码拿出来,在iOS7的机器上运行,发现键盘上的ReturnKeyType不能被修改了. 经过几番查找资料,了解到iOS7中UISearchBar的结构发生了变化,将 ...
- js和jquery获取属性的区别
一.获取元素: js获取元素: 根据id获取:document.getElementById("id"); 根据类名获取:document.getElementsByClassNa ...
- MVC 提交List 集合 注意对应的参数名称
public void AddMovieInfos(List<MoviesInfo> movies) { foreach (var item in movies) { dal.Add(it ...
- NVIDIA TX1/TX2 对比
处理器方面,TX2由TX1的Tegra X1升至Tegra Parker处理器,该处理器由16nm工艺制造,6核心设计,CPU部分由2个丹佛+4个A57核心共同组成. GPU则采用Pascal架构,拥 ...