web-api和swagger文档(web-api & swagger documentation)
我写了一个asp web-api,并且使用这个类添加了对api的版本控制。
我的路线如下所示:
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{namespace}/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));
所以我可以访问
https://localhost:44301/v1/test
我现在试图添加Swagger来生成一些api文档(实际上我使用的是Swashbuckle Nuget包)
当我浏览
/swagger
放大页面加载但为所有控制器出现错误时:Unable to read api 'TestController' from path https://localhost:44301/swagger/api-docs/TestController (server returned undefined)
如果我浏览到
https://localhost:44301/swagger/api-docs/TestController
它会出现一个NullReferenceException
,堆栈跟踪为:at Swashbuckle.Swagger.OperationGenerator.CreateParameter(ApiParameterDescription apiParamDesc, String apiPath) at Swashbuckle.Swagger.OperationGenerator.<>c__DisplayClass2.<ApiDescriptionToOperation>b__1(ApiParameterDescription paramDesc) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Swashbuckle.Swagger.OperationGenerator.ApiDescriptionToOperation(ApiDescription apiDescription) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Swashbuckle.Swagger.ApiExplorerAdapter.CreateApi(IGrouping`2 apiDescriptionGroup, OperationGenerator operationGenerator) at Swashbuckle.Swagger.ApiExplorerAdapter.<>c__DisplayClassb.<GetDeclaration>b__7(IGrouping`2 apiDescGrp) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Swashbuckle.Swagger.ApiExplorerAdapter.GetDeclaration(String basePath, String version, String resourceName) at Swashbuckle.Application.CachingSwaggerProvider.<>c__DisplayClass4.<GetDeclaration>b__3(String k) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at Swashbuckle.Application.CachingSwaggerProvider.GetDeclaration(String basePath, String version, String resourceName) at Swashbuckle.Application.SwaggerSpecHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.HttpServer.<>n__FabricatedMethod9(HttpRequestMessage , CancellationToken ) at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()
如果我将路线更改为:
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new { id = RouteParameter.Optional } )
Swagger页面然后工作,但实际的API不!
所以问题是,我如何让Swagger使用版本化的API?
I've written a asp web-api and I've added versioning to the api using this class.
My routes look like this:
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{namespace}/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));
So I can access
https://localhost:44301/v1/test
I'm now trying to add Swagger to produce some api documentation (actually I'm using the Swashbuckle Nuget package)
When I browse to
/swagger
the page loads but comes up with errors for all the controllers:Unable to read api 'TestController' from path https://localhost:44301/swagger/api-docs/TestController (server returned undefined)
If I browse to
https://localhost:44301/swagger/api-docs/TestController
it comes up with aNullReferenceException
with a stack trace of:at Swashbuckle.Swagger.OperationGenerator.CreateParameter(ApiParameterDescription apiParamDesc, String apiPath) at Swashbuckle.Swagger.OperationGenerator.<>c__DisplayClass2.<ApiDescriptionToOperation>b__1(ApiParameterDescription paramDesc) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Swashbuckle.Swagger.OperationGenerator.ApiDescriptionToOperation(ApiDescription apiDescription) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Swashbuckle.Swagger.ApiExplorerAdapter.CreateApi(IGrouping`2 apiDescriptionGroup, OperationGenerator operationGenerator) at Swashbuckle.Swagger.ApiExplorerAdapter.<>c__DisplayClassb.<GetDeclaration>b__7(IGrouping`2 apiDescGrp) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at Swashbuckle.Swagger.ApiExplorerAdapter.GetDeclaration(String basePath, String version, String resourceName) at Swashbuckle.Application.CachingSwaggerProvider.<>c__DisplayClass4.<GetDeclaration>b__3(String k) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at Swashbuckle.Application.CachingSwaggerProvider.GetDeclaration(String basePath, String version, String resourceName) at Swashbuckle.Application.SwaggerSpecHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) at System.Web.Http.HttpServer.<>n__FabricatedMethod9(HttpRequestMessage , CancellationToken ) at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()
If I change the route to this:
config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new { id = RouteParameter.Optional } )
The Swagger pages then work, but the actual api doesn't!
So the question is, how can I make Swagger work with a versioned api?
原文:https://stackoverflow.com/questions/24779180
最满意答案
最后我使用了
SDammann.WebApi.Versioning
nuget包和以下路由:VersionedControllerSelector.VersionPrefix = "v"; config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "v{version}/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );
我的控制器位于名为v1或v2的名称空间中。
In the end I used
SDammann.WebApi.Versioning
nuget package and the following routes:VersionedControllerSelector.VersionPrefix = "v"; config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "v{version}/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );
My controllers are in a namespace called v1 or v2 etc.
相关问答
更多-
“Web API帮助页面”包(Microsoft.AspNet.WebApi.HelpPage)构建在ApiExplorer之上。 Web API帮助页面生成文档页面并将它们添加到您的项目中。 ApiExplorer为您提供了一个较低级别的API,它只提供API描述,您可以使用它来创建文档。 请参阅: http : //www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages 我没有用过Swagger。 从自述中,它也构建在 ...
-
好的,找到了。 这不是理想的解决方案 - 但需要前端(网络和移动)开发人员 - 并且它可以完成这项工作。 基本上最新的DRF和Swagger使用rest_framework.schemas import SchemaGenerator来为Swagger提供文档。 所以我需要对它进行一点延伸: # -*- coding: utf-8 -*- import urlparse import coreapi from rest_framework.schemas import SchemaGenerator ...
-
我已经确定这是Swashbuckle中的一个错误。 .NET使用实际方法名称生成XML文档,而不是ActionName属性中指定的名称。 因此,它需要在Swashbuckle代码中处理。 我在GitHub中输入了一个问题,包括一个解决方法,直到它被修复 - https://github.com/domaindrivendev/Swashbuckle/issues/118 。 I've determined this is a bug in Swashbuckle. .NET generates the XM ...
-
在我的脑中添加这些语句后问题就解决了 春天-config.xml中
如何将Swagger页面设置为web api 2项目的默认登录页面?(How to set Swagger page as default landing page for web api 2 project?)[2021-04-10]
您可以在此处创建重定向,请参阅和示例: http://turoapi.azurewebsites.net/ 我所做的就是用以下代码创建一个index.html:有了这 ... 如何在hapijs中禁用生产服务器上的swagger API文档(How to disable swagger API documentation from production server in hapijs)[2021-07-19]
根据文档,您可以通过在注册期间设置选项来关闭documentationPage 。 该文档还说明了如何使用选项注册插件。 const hapiSwaggerOptions = { info: { title: 'Documentation', version: '1.0.0', description: 'This is the API' }, documentationPage: process.env.NODE_ENV !== 'product ...最后我使用了SDammann.WebApi.Versioning nuget包和以下路由: VersionedControllerSelector.VersionPrefix = "v"; config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "v{version}/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 我的 ...我找到了解决这个问题的方法。 我应该摆脱所有的依赖和额外的配置,并使其更简单。UTF-8 1.8 ${project.build.directory}/generated-docs/swagger ...我最终在ResponseWrappingHandler添加了这段代码。 可能有更优雅的方式,但这对我有用。 这会导致swagger页面请求退出FluentValidation挂钩。 if (request.RequestUri.AbsolutePath.Contains("swagger")) { return response; } I ended up adding this code inside ResponseWrappingHandler. There may be a more el ...我认为这可以从throw消息中自我解释Please enable \"XML documentation file\" in project properties with default (bin\TGSanokeData.XNK) value or edit value in App_Start\Swaggernet.cs"); 您可以在XML文件中进行更改,或者调用GlobalConfiguration.Configuration.EnableSwagger和GlobalConfiguration.C ...相关文章
更多- springboot整合Swagger2
- Swagger UI 快速入门-springmvc 整合Swagger UI 教程
- 自制公众平台Web Api(微信)
- API调用太麻烦 eBay推出Web查询语言
- 使用jQuery、Yahoo API和HTML5的geolocation来开发一个天气预报web应用
- web storm使用和配置
- Python中有没有类似于JAVA的API那样的完整的查询文档
- JAVA WEB
- POI 操作 Excel的主要API
最新问答
更多- 如何重新加载下拉列表(How to reload dropdownlist)
- RStudio:在脚本中保留特殊字符(RStudio: keeping special characters in a script)
- Powershell的“GetLatest”不会在新的TFS工作区上下载文件(“GetLatest” with Powershell doesn't download files on new TFS workspace)
- 我如何让JS识别一个由字符组成的数组?(How do I get JS to recognise an array insted of characters?)
- EF从存储过程中急切加载(EF eager loading from stored procedure)
- 将输出文件添加到Python扩展(Adding output file to Python extension)
- 淮北职业技术学院电脑应用专业咋样?
- 更改默认扩展面板箭头的箭头样式(Change arrow style for default expansion panel arrow)
- 芜湖计算机(计算机)培训机构(培训班,学校)哪家好
- 致命错误:使用clang-llvm ASTMatcher时未找到'stddef.h'文件(fatal error: 'stddef.h' file not found when using clang-llvm ASTMatcher)
- 内容的.NET缓存(Contentful .NET caching)
- 客户端没有发生WCF回调(WCF callback is not happening in client)
- 使用friend在全局范围内调用类成员函数会产生27个错误(Calling a Class member function in Global Scope using friend Gives 27 ERRORS)
- 如何绑定到WPF中的另一个控件属性(How to Bind to Another Control Property in WPF)
- 南华大学电脑专业,就业好不好
- 是否存在泄密文件的官方(或常见)文件扩展名或后缀?(Is there an official (or common) file extention or suffix for deflated files?)
- 在SVM python中只训练一次(Training only once in SVM python)
- 淘汰自定义绑定光滑js无法正常工作(knockout custom binding for slick js not working)
- 似乎无法正确地抓住网站“福布斯”(Can't seem to scrape the website “Forbes” properly)
- 无法使用boto.rds2从describe_instance方法检索有关db实例的信息(Not able to retrieve information about db instances from the describe_instance method using boto.rds2)
- 转换为英国日期格式问题(Convert to british date format issue)
- 在表中列出不同的元组(10种方法)(List distinct tuples in a table(SQL query)(10 ways))
- OrientDB查询比较(OrientDB query compare)
- 全局变量有什么不好?(What is so bad about global variables? [duplicate])
- 为什么JavaMail Transport.send()是一个静态方法?(Why is JavaMail Transport.send() a static method?)
- 获取最近3个Instagram图像张贴在一个地方(Get last 3 instagram images posted in a place)
- 使用libnfc格式化/读/写NDEF Mifare 1K卡(Format/Read/Write NDEF Mifare 1K Card using libnfc)
- 阻止谷歌索引特定图像(Block Google from indexing a particular image)
- 消息模板接收让Dispatcher没有订阅频道(Message Template receive gives Dispatcher has no subscribers for channel)
- OpenShift:使用自定义节点版本(OpenShift: Use custom node version)