关于 ASP.NET Core 中的 OData
1. BooksController
using BooksODataService.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNet.OData.Query;
using Microsoft.EntityFrameworkCore;
using System.Linq; namespace BooksODataService.Controllers
{
public class BooksController : ODataController
{
private readonly BooksContext _booksContext;
public BooksController(BooksContext booksContext)
{
_booksContext = booksContext;
} [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<Book> Get(ODataQueryOptions options)
{
ODataValidationSettings settings = new ODataValidationSettings()
{
MaxExpansionDepth =
};
options.Validate(settings);
var books = _booksContext.Books.Include(b => b.Chapters);
return books;
} [EnableQuery()]
public SingleResult<Book> Get([FromODataUri] int key)
{
return SingleResult.Create(_booksContext.Books.Where(b => b.Id == key));
} } }
2. ChaptersController
using BooksODataService.Models;
using Microsoft.AspNet.OData;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; namespace BooksODataService.Controllers
{
public class ChaptersController : ODataController
{
private readonly BooksContext _booksContext;
public ChaptersController(BooksContext booksContext)
{
_booksContext = booksContext;
} public IQueryable<BookChapter> Get() =>
_booksContext.Chapters.Include(c => c.Book); [EnableQuery]
public SingleResult<BookChapter> Get([FromODataUri] int key) =>
SingleResult.Create(_booksContext.Chapters.Where(c => c.Id == key)); }
}
3. Startup.cs
using BooksODataService.Models;
using BooksODataService.Services;
using Microsoft.AspNet.OData.Builder;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; // OData Beta 1 docu http://odata.github.io/WebApi/#14-01-netcore-beta1 namespace BooksODataService
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddTransient<CreateBooksService>();
services.AddDbContext<BooksContext>(options =>
{
string connString = Configuration.
GetConnectionString("BooksConnection");
options.UseSqlServer(connString);
});
services.AddOData();
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env, CreateBooksService sampleBooks)
{
sampleBooks.CreateDatabase();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
var builder = new ODataConventionModelBuilder(app.ApplicationServices);
builder.EntitySet<Book>("Books");
builder.EntitySet<BookChapter>("Chapters");
app.UseMvc(routeBuilder =>
{
routeBuilder.MapODataServiceRoute("ODataRoute",
"odata", builder.GetEdmModel());
routeBuilder.EnableDependencyInjection(); // workaround for Beta 1
});
}
}
}
运行截图:



代码下载:https://files.cnblogs.com/files/Music/ODataSample-In-ASPNET-Core.rar
关于 ASP.NET Core 中的 OData的更多相关文章
- ASP.NET Core 中的那些认证中间件及一些重要知识点
前言 在读这篇文章之间,建议先看一下我的 ASP.NET Core 之 Identity 入门系列(一,二,三)奠定一下基础. 有关于 Authentication 的知识太广,所以本篇介绍几个在 A ...
- Asp.net Core中使用Session
前言 2017年就这么悄无声息的开始了,2017年对我来说又是特别重要的一年. 元旦放假在家写了个Asp.net Core验证码登录, 做demo的过程中遇到两个小问题,第一是在Asp.net Cor ...
- 在ASP.NET Core中使用百度在线编辑器UEditor
在ASP.NET Core中使用百度在线编辑器UEditor 0x00 起因 最近需要一个在线编辑器,之前听人说过百度的UEditor不错,去官网下了一个.不过服务端只有ASP.NET版的,如果是为了 ...
- ASP.NET Core中的依赖注入(1):控制反转(IoC)
ASP.NET Core在启动以及后续针对每个请求的处理过程中的各个环节都需要相应的组件提供相应的服务,为了方便对这些组件进行定制,ASP.NET通过定义接口的方式对它们进行了"标准化&qu ...
- ASP.NET Core中的依赖注入(2):依赖注入(DI)
IoC主要体现了这样一种设计思想:通过将一组通用流程的控制从应用转移到框架之中以实现对流程的复用,同时采用"好莱坞原则"是应用程序以被动的方式实现对流程的定制.我们可以采用若干设计 ...
- ASP.NET Core中的依赖注入(3): 服务的注册与提供
在采用了依赖注入的应用中,我们总是直接利用DI容器直接获取所需的服务实例,换句话说,DI容器起到了一个服务提供者的角色,它能够根据我们提供的服务描述信息提供一个可用的服务对象.ASP.NET Core ...
- ASP.NET Core中的依赖注入(4): 构造函数的选择与服务生命周期管理
ServiceProvider最终提供的服务实例都是根据对应的ServiceDescriptor创建的,对于一个具体的ServiceDescriptor对象来说,如果它的ImplementationI ...
- ASP.NET Core 中文文档 第二章 指南(4.6)Controller 方法与视图
原文:Controller methods and views 作者:Rick Anderson 翻译:谢炀(Kiler) 校对:孟帅洋(书缘) .张仁建(第二年.夏) .许登洋(Seay) .姚阿勇 ...
- ASP.NET Core 中文文档 第三章 原理(1)应用程序启动
原文:Application Startup 作者:Steve Smith 翻译:刘怡(AlexLEWIS) 校对:谢炀(kiler398).许登洋(Seay) ASP.NET Core 为你的应用程 ...
随机推荐
- java 主动信任证书
java 主动信任证书 SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mid.toCharArray() ...
- [译]Vulkan教程(09)窗口表面
[译]Vulkan教程(09)窗口表面 Since Vulkan is a platform agnostic API, it can not interface directly with the ...
- java之模板方法设计模式
抽象类体现的就是一种模板模式的设计.抽象类作为多个子类的通用模板.子类在抽象类的基础上进行扩张和改造,但子类总体上会保留抽象类的行为方式. 解决的问题: 当功能内部一部分实现是确定的,一部分实现是不确 ...
- .NET多线程知识快速学习
多线程是一个不会过时的话题,因为每个开发的成长必然要掌握这个知识点,否则半懂不懂怎么保证系统的可靠性和性能,其实在网上随便一搜都会有海量的文章说这个话题,大多数写得很细写得非常好,但发现很少有概览性的 ...
- C# 波浪线绘制
波浪线效果如上 界面绘制操作 private Point? _startPoint = null; private void ContainerCanvas_OnPreviewMouseLeftBut ...
- ASP.NET Core 2.2 WebApi 系列【七】泛型仓储模式和工作单元
在之前的泛型仓储模式实现中,每个增删改都调用了SaveChanges方法,导致每次更新都提交了事务. 在实际开发过程中,我们经常遇到同时操作多张表数据,那么按照之前的写法,对数据库提交了多次操作,开启 ...
- Selenium(六):frame切换、窗口切换
1. 切换到frame index.html: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- msyql master thread
------------------------------------------------------ 2015-02-10----------------------------------- ...
- MySQL 优化 (二)
参数优化 Max_connections (1)简介 Mysql的最大连接数,如果服务器的并发请求量比较大,可以调高这个值,如果连接数越来越多,mysql会为每个连接提供单独的缓冲区,就会开销的越多的 ...
- 安装 openmpi 4.0 用于 horovod 编译
最近编译 horovod框架过程中,需要使用openmpi 4.0但是环境中的openmpi版本比较低,所以在手动安装openmpi4.0 用于编译,下面对过程进行简要记录,进行备忘: curl -O ...