OData – Routing
前言
以前我都是把 ODataController 和普通 API Controller 分开做. (因为 OData 实在多 Bug, 好东西尽量不要掺和它)
Read 的部分用 OData, CUD 的部分就用原本的 API.
但最近 OData 弃用了 ODataRoute 这些东西, 改用 Web API 的 Route, HttpGet 等.
视乎它有心把大家统一起来, 所以我去了解了一下.
主要参考
Routing in ASP.NET Core OData 8.0 Preview
Attribute Routing in ASP.NET Core OData 8.0 RC
By Default
在没有设置 Attribute 的情况下. 当我们定义一个 Entity 以后. 它就有了它的路径
builder.EntitySet<Product>("Products");
比如
GET products
GET products/$count
GET products/{key}
GET products({key})
这个叫 template, 一般上我们是不会去修改它的. 改了就不像 restful 了嘛.
Controller 要对上这些路径, 就要有一套标准写法

有一点点写不对就匹配不上了
Attribute route
Web API 的 attribute route 可以让我们任意 match url 然后带去 action.
OData attribute route 只是取代了上面那种通过 Controller 命名去 match 的方式.
它并不能任意写 url 哦, 你还是得按照 OData 规定得 restful template 去写匹配

在使用 attribute route 之后, controller, action, parameter 的命名就可以任意了.
但要记得哦, 它不能任意写 path, 一定要写 odata restful template 定义好的, 除非你自己去 override route conversion.
和 WebAPI share 同一个 Controller
我不知道这个是不是一个好的 way 啦. 但至少分开 Controller 对 swagger 会是一个问题啦 (它是以 Controller 分 group 的)
首先不要继承 ODataController

它里面其实也只是有 created 和 updated 的东西而已, 而我刚好只用它做 query. perfect !
没有 ODataController 以后 attribute route 就不 working 了
需要另外加上 [ODataAttributeRouting]
[ODataAttributeRouting]
public class HandleCustomerController : Controller
{
…
}
或者
[ODataAttributeRouting]
[HttpGet("odata/Orders/{key}")]
public IActionResult Get(int key)
{
return Ok($"Orders{key} from OData");
}
如果只是 action 有, controller 没有, 但是它也会去 parent 拿到 api 哦. 很聪明一下

但是!!我更推荐把 [ODataAttributeRouting] 放到 Controller,然后在不需要 OData 的 Action 上添加 [ODataIgnored]。
因为我在做 OData Versioning 的时候中过一个 bug,我只把 [ODataAttributeRouting] 放在 Action 结果它这次不聪明了,必须放到 [ODataAttributeRouting] 才行,具体原因我不清楚,但是我觉得还是不要依赖它的聪明比较好。
OData – Routing的更多相关文章
- 在ASP.NET Web API中使用OData
http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...
- 重点关注之OData with List
OData是什么 官方解释:The Open Data Protocol (OData) is a data access protocol for the web. OData provides a ...
- ASP.NET Web API中使用OData
在ASP.NET Web API中使用OData 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在ASP.NET Web API中,对于CRUD(creat ...
- 快速搭建WebAPI(Odata+Code-First)附Odata条件查询表~
Odata是什么? 开放数据协议(Open Data Protocol,缩写OData)是一种描述如何创建和访问Restful服务的OASIS标准.该标准由微软发起,前三个版本1.0.2.0.3.0都 ...
- [转]Support Composite Key in ASP.NET Web API OData
本文转自:https://code.msdn.microsoft.com/Support-Composite-Key-in-d1d53161 he default EntitySetControlle ...
- [转]Web API OData V4 Keys, Composite Keys and Functions Part 11
本文转自:https://damienbod.com/2014/09/12/web-api-odata-v4-keys-composite-keys-and-functions-part-11/ We ...
- [转]ASP.NET web API 2 OData enhancements
本文转自:https://www.pluralsight.com/blog/tutorials/asp-net-web-api-2-odata-enhancements Along with the ...
- [转]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- ...
- [转]Getting started with ASP.NET Web API OData in 3 simple steps
本文转自:https://blogs.msdn.microsoft.com/webdev/2013/01/29/getting-started-with-asp-net-web-api-odata-i ...
- [转]Using OData from ASP.NET
本文转自:http://www.drdobbs.com/windows/using-odata-from-aspnet/240168672 By Gastón Hillar, July 01, 201 ...
随机推荐
- 如何去除字符串中的 "\n" ?80% 的同学错了!
大家好,我是鱼皮,今天分享一个小知识. 我最近负责的工作是设计一个 SQL 解析引擎.简单来说,就是将一个 SQL 表达式字符串,解析为一颗对象树,从而执行查询等一系列操作. 在最开始,我就遇到了一个 ...
- TP3.2与TP5.0的区别
1. 控制器输出return $this->fetch(); ----5$this->display(); ----3.2单字母函数去掉了 如:M() D() U() S() C() 3. ...
- oeasy教您玩转vim - 6 - # 保存修改
另存与保存 回忆上节课内容 我们上次进入了插入模式 从正常模式,按<kbd>i</kbd>,进插入模式 从插入模式,按<kbd>ctrl</kbd>+& ...
- 广州大学第十八届ACM大学生程序设计竞赛(同步赛)——题解
这套题我答的很失败.没有按照题目的难度去答题,前期浪费了不少时间. 题目: A-字符画 题解:思维.模拟.这道题我的通过率为62.5,没有过的原因是因为对细节的处理和把控不到位,对一些点忽视,我也记录 ...
- Win10下安装LabelImg以及使用--LabelImg
labelImg是图片标注软件,用于数据集的制作.标注等等.下面介绍labelImg的安装过程. 我用的是anaconda,所以以anaconda prompt作为终端: 在Anaconda Prom ...
- 基于vue.js 移动可视化,拖拽生成H5系统
效果预览 功能简介 基于Vue.js(2.0)版本开发的,通过拖拽可视化的操作,生成H5的页面,类似易企秀的工具,前端展示页面运用了之前发布的 vue-animate-fullpage 插件进行动画渲 ...
- 很呆的一个问题:我的新项目又找不到mapper这个bean了
1.选springboot版本 <properties> <java.version>8</java.version> <project.build.sour ...
- linux环境部署搭建流程
linux环境部署搭建流程 1,ubuntu/centos系统安装 2,ip网络配置 3,路由和防火墙 4,时间同步服务器 5,ssh协议配置(Windows安装xshell/secureCRT) 6 ...
- 【Tutorial C】03 数据类型、变量
在程序的世界中,可以让计算机按照指令做很多事情, 如进行数值计算.图像显示.语音对话.视频播放.天文计算.发送邮件.游戏绘图以及任何我们可以想象到的事情. 要完成这些任务,程序需要使用数据,即承载信息 ...
- 基于MybatisPlus的简单分页查询和条件分页查询
分页查询 分析: 分析文档要求 查看前端传递给后台的参数 分析参数进行编码 后台返回给前端的数据 思路 浏览器 - > Controller层 - > Service层 - > Ma ...