NSwag在asp.net web api中的使用,基于Global.asax
https://github.com/NSwag/NSwag/wiki/OwinGlobalAsax
This page explains how to use the NSwag OWIN middleware in your "Global.asax"-based web project.
It is recommended to migrate your project to a completely OWIN-based project and use the OWIN middleware directly.
Sample project https://github.com/RSuter/NSwag/tree/master/src/NSwag.Sample.NetGlobalAsax
https://github.com/RSuter/NSwag/tree/master/src/ 这个路径下还有一些其他的demo
Integration
1. Install NuGet packages
Install the NuGet packages:
其中Microsoft.Owin.Host.SystemWeb依赖于另外2个
Microsoft.Owin (>= 4.0.0)
Owin (>= 1.0.0)
其中NSwag.AspNet.Owin的依赖如下
- Microsoft.Owin (>= 3.0.1)
- Microsoft.Owin.StaticFiles (>= 3.0.1)
- NJsonSchema (>= 9.13.15)
- NSwag.Core (>= 12.0.12)
- NSwag.SwaggerGeneration (>= 12.0.12)
- NSwag.SwaggerGeneration.WebApi (>= 12.0.12)
- Owin (>= 1.0.0)
2. Edit web.config
Then open your Web.config and add the following app setting:
<configuration>
<appSettings>
<add key="owin:AutomaticAppStartup" value="false" />
</appSettings>
...
Now we need setup the routing of the Swagger requests. There are two ways to do this:
2.a) Pipe all request to the .NET pipeline
In the system.webServer tag, set runAllManagedModulesForAllRequests to true so that all requests are piped to ASP.NET:
<system.webServer>
...
<modules runAllManagedModulesForAllRequests="true">
...
2.b) Pipe only the Swagger request to the specific middlewares
Important: The routes defined in the web.config and the UseSwagger/UseSwaggerUi methods must be the same:
<system.webServer>
...
<handlers>
...
<add name="NSwag" path="swagger" verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
3. Edit Global.asax.cs
Now, open the Global.asax.cs and add the following call at the beginning of the Application_Start method:
/// <summary>
/// https://blog.rsuter.com/nswag-tutorial-integrate-the-nswag-toolchain-into-your-asp-net-web-api-project/
/// https://github.com/RSuter/NSwag/wiki/OwinGlobalAsax
/// </summary>
/// <param name="config"></param>
public static void RegisterNSwag()
{
RouteTable.Routes.MapOwinPath("swagger", app =>
{
app.UseSwaggerUi(typeof(WebApiApplication).Assembly, settings =>
{
settings.MiddlewareBasePath = "/swagger";
});
});
}
Now, start the web project and browse to "http:/localhost:port/swagger" and the Swagger UI should be loaded.
添加之后遇到的问题:
Server Error in '/LISA.WebApi.Chile' Application.
The method 'post' on path '/api/CodeStocks' is registered multiple times (check the DefaultUrlTemplate setting [default for Web API: 'api/{controller}/{id}'; for MVC projects: '{controller}/{action}/{id?}']).
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The method 'post' on path '/api/CodeStocks' is registered multiple times (check the DefaultUrlTemplate setting [default for Web API: 'api/{controller}/{id}'; for MVC projects: '{controller}/{action}/{id?}']).
https://github.com/RSuter/NSwag/issues/664
@nkm8 See domaindrivendev/Swashbuckle#142 (comment) for some background information about the query parameters not being part of the path:
https://github.com/domaindrivendev/Swashbuckle/issues/142#issuecomment-66492163
Unfortunately this is a constraint imposed by the Swagger specification https://github.com/swagger-api/swagger-spec/blob/master/versions/2.0.md.
I had some late involvement with the Swagger 2.0 working group and pushed hard to have this constraint removed but to no avail. As you pointed out, this does cause an issue for certain WebApi implementations. I'll describe the workaround below but first I want to play devil's advocate and look at it from an API perspective, agnostic of implementation frameworks or even specific language constructs.
In it's essence, the constraint really just says the following paths have to be described as one operation.
GET api/products
GET api/products?producType={productType}
Behind the scenes these may be implemented as separate C# or separate
Java methods, but in the context of describing a REST API, would you
describe them separately? Swagger 2.0 says "No ... it's one operation
with an optional productType parameter".
Technically, I believe they are two different resources (hence why I
opposed the constraint) but I do see some sense in the Swagger 2.0
approach. For example, I don't think I've ever seen any API docs broken
down this way - it's invariably just by path with additional information
about query parameters included.
Anyway, philosophy aside - breaking the Swagger 2.0 spec is not an option and so I can only provide some workarounds.
The most straight forward would be to consolidate your multiple,
"overloaded", actions with a single action with optional parameters. You
could even delegate internally to the private overloaded versions.
If a change to the implementation isn't an option - Swashbuckle 5.0 provides a config setting ResolveConflictingActions.
This takes a function of the form -
Func<<IEnumerable<ApiDescription>, ApiDescription> which
you can provide to consolidate the actions into one ApiDescription, and
therefore one Operation, at the documentation level only.
This "merge" process will get trickier if the response type and
errors codes also differ but at that point you'd have to ask questions
about the API design.
Hope this helps - let me know if it makes sense?
https://github.com/RSuter/NSwag/issues/1242
app.UseSwaggerUi3(typeof(Startup).Assembly, settings =>
{
settings.GeneratorSettings.DefaultPropertyNameHandling = PropertyNameHandling.CamelCase;
settings.GeneratorSettings.DefaultUrlTemplate = "{controller=Home}/{action=Index}/{locale?}";
settings.GeneratorSettings.IsAspNetCore = true;
});
最后搭建成功的效果

多个controller的效果图

NSwag在asp.net web api中的使用,基于Global.asax的更多相关文章
- ASP.NET Web API中的Controller
虽然通过Visual Studio向导在ASP.NET Web API项目中创建的 Controller类型默认派生与抽象类型ApiController,但是ASP.NET Web API框架本身只要 ...
- 在ASP.NET Web API中使用OData
http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...
- ASP.NET Web API 中的异常处理(转载)
转载地址:ASP.NET Web API 中的异常处理
- 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化
谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...
- Asp.Net Web API 2第十三课——ASP.NET Web API中的JSON和XML序列化
前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...
- ASP.NET WEB API 中的路由调试与执行过程跟踪
路由调试 RouteDebugger 是调试 ASP.NET MVC 路由的一个好的工具,在ASP.NET WEB API中相应的有 WebApiRouteDebugger ,Nuget安装 Inst ...
- 能省则省:在ASP.NET Web API中通过HTTP Headers返回数据
对于一些返回数据非常简单的 Web API,比如我们今天遇到的“返回指定用户的未读站内短消息数”,返回数据就是一个数字,如果通过 http response body 返回数据,显得有些奢侈.何不直接 ...
- ASP.NET Web API中的参数绑定总结
ASP.NET Web API中的action参数类型可以分为简单类型和复杂类型. HttpResponseMessage Put(int id, Product item) id是int类型,是简单 ...
- 【ASP.NET Web API教程】5.5 ASP.NET Web API中的HTTP Cookie
原文:[ASP.NET Web API教程]5.5 ASP.NET Web API中的HTTP Cookie 5.5 HTTP Cookies in ASP.NET Web API 5.5 ASP.N ...
随机推荐
- Eclipse获取签名证书的SHA1
签名后的获取方式 :http://jingyan.baidu.com/article/3ea51489da3bc252e71bba59.html 未签名的获取方式:http://www.th7.cn/ ...
- 170306、wamp中的Apache开启gzip压缩提高网站的响应速度
一个网站的响应速度决定该网站的人气和质量,所以wamp配置的服务器也需要支持giz压缩来提高网站的响应速度,如何开启wamp的gzip压缩呢,经过在网站查找资料结合自己服务器中的配置,现在将这个方法分 ...
- 求其中同一个主叫号码的两次通话之间间隔大于10秒的通话记录ID
求其中同一个主叫号码的两次通话之间间隔大于10秒的通话记录ID 例如:6,7,8,9,10条记录均符合 ID 主叫号码 被叫号码 通话起始时间 通话结束时间 ...
- 《JAVA多线程编程核心技术》 笔记:第一章
一.基本概念理解:1.1.进程和线程的理解1.2.同步和异步的理解(阻塞模式和非阻塞模式)1.3 线程间共享变量和不共享变量二.多线程的实现方式和构造方法:2.1 实现方式:2个2.2 构造方法:8个 ...
- jd算法大赛 一个user_id只需映射到一个sku_id, 但是一个sku_id能否映射到多个user_id
0-购买预测 w 任务目标 提交user_id-->sku_id (1->1,但反向呢?) 实际操作: 0- 行为表中的type值加权为一个购买意向值0(性别.年龄,不干扰/或加权) 品类 ...
- 无NavigationBar到有NavigationBar视图切换时的一个坑
NavigationController在iOS App中是最常见不过了,可以说是每个App中必备的了.自iOS7开始,系统自带的右滑返回效果,也可以让有NavigationBar的视图切换很丝滑流畅 ...
- Python菜鸟之路:Django 缓存
Django中提供了6种缓存方式: 开发调试 内存 文件 数据库 Memcache缓存(python-memcached模块) Memcache缓存(pylibmc模块) 1. 开发调试 # 此为开始 ...
- 利用Docker快速部署Oracle环境
工作中需要频繁使用Oracle环境,但是每次搭建起来比较消耗时间,本想通过虚拟机模板的方式来快速安装oracle vm,但是每次改ip等环境也很耗时,因此想到docker中有没有已经做好的images ...
- 简单的 H5 视频推流解决方案
导语 随着直播平台爆发式增长,直播平台从 PC 端转战移动端,紧跟着直播的潮流,自己学习实现了一套简单的 H5 视频推流的解决方案,下面就给小伙伴们分享一下自己学习过程中的经验. 环境部署 1. 配置 ...
- 简明python教程五----数据结构
python中有三种内建的数据结构:列表.元组和字典 list是处理一组有序项目的数据结构,即你可以在一个列表中存储一个序列的项目.在python中,每个项目之间用逗号分隔. 列表中的项目应该包括在方 ...