基于 WebAPI 的 API 实现
2016-07-28 16:29
369 查看
本文基于
WebAPI
OData (微软发起的一个格式标准,其中一个比较有意思的是可以直接在 Excel 中填入 API 就可以展示了)
Swashbuckle.OData(把 API 生成一个测试页面)
项目结构
View Code
Tips:
53515 是项目设置 Web 选项里的端口号,具体请查询真实的端口号
勾选项目设置 Build 选项里的XML documentation file,因为 Swagger 会用 xml 里的注释作为说明,请把代码中的 /// 注释补全,这样才能生成说明
![](http://images2015.cnblogs.com/blog/280776/201607/280776-20160728162139997-29825529.png)
运行结果
由于测试直接返回的固定数据,所以,当运行后就可以通过 API 得到结果了,并且可以通过 Swagger 测试页面看到 API 的信息,并可以测试。
此例中的 API 为
http://localhost:53515/Users?$orderby=Id desc (请求的 API)
http://localhost:53515/swagger/ui/index#/ (Swagger 生成的测试页面)
WebAPI
OData (微软发起的一个格式标准,其中一个比较有意思的是可以直接在 Excel 中填入 API 就可以展示了)
Swashbuckle.OData(把 API 生成一个测试页面)
项目结构
public class EfContext : DbContext { /// <summary> /// Users /// </summary> public DbSet<UserModel> Users { get; set; } /// <summary> /// EfContext ctor /// </summary> public EfContext() : base("name=EfContext") { } /// <summary> /// This method is called when the model for a derived context has been initialized, but /// before the model has been locked down and used to initialize the context. The default /// implementation of this method does nothing, but it can be overridden in a derived class /// such that the model can be further configured before it is locked down. /// </summary> /// <remarks> /// Typically, this method is called only once when the first instance of a derived context /// is created. The model for that context is then cached and is for all further instances of /// the context in the app domain. This caching can be disabled by setting the ModelCaching /// property on the given ModelBuidler, but note that this can seriously degrade performance. /// More control over caching is provided through use of the DbModelBuilder and DbContextFactory /// classes directly. /// </remarks> /// <param name="modelBuilder">The builder that defines the model for the context being created. </param> protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Configurations.Add(new UserModelMap()); } }
View Code
Tips:
53515 是项目设置 Web 选项里的端口号,具体请查询真实的端口号
勾选项目设置 Build 选项里的XML documentation file,因为 Swagger 会用 xml 里的注释作为说明,请把代码中的 /// 注释补全,这样才能生成说明
![](http://images2015.cnblogs.com/blog/280776/201607/280776-20160728162139997-29825529.png)
运行结果
由于测试直接返回的固定数据,所以,当运行后就可以通过 API 得到结果了,并且可以通过 Swagger 测试页面看到 API 的信息,并可以测试。
此例中的 API 为
http://localhost:53515/Users?$orderby=Id desc (请求的 API)
http://localhost:53515/swagger/ui/index#/ (Swagger 生成的测试页面)
![](http://images2015.cnblogs.com/blog/280776/201607/280776-20160728162742309-1833674189.png)
相关文章推荐
- python——周边
- 【HEVC帧间预测论文】P1.2 An Efficient Inter Mode Decision Approach for H.264 Video Codin
- OSG帮助文档生成
- Insight task:annotation-driven 解析 | Insight 常识
- Faster R-CNN系列之PYTHON篇
- hihoCoder :Memory Allocating Algorithm
- HDU 4864 Task
- java语言基础入门——字符串两种建立方式的比较
- java按一定数量补0,随机数获取
- return x与exit(x)到底有什么区别
- Git 进阶 —— 远程仓库
- Selenium源码分析之WebDriver
- 2116数据结构实验之链表一:顺序建立链表
- 2016最新最全的App上架AppStore教程
- 使用ngrok让微信公众平台通过80端口访问本机
- POJ 2386 Lake Counting(水淹菜地,DFS,八连通,连通分量)
- poj2251 Dungeon Master 简单BFS
- 移动端设计规范
- 【Android笔记】Service的生命周期
- Android:控件WebView显示网页