Swagger 2.0不支持:带有路径的多个操作 [英] Not supported by Swagger 2.0: Multiple operations with path
问题描述
我已经在WebApi 2应用程序中集成了招摇工具.当应用程序具有单个控制器时,它可以正常工作. 当我在应用程序中添加第二个控制器时.我收到以下错误:
I have integrated swagger in WebApi 2 application. It works fine when application has single controller. When I added second controller in the application. I got following error :
发生错误.," ExceptionMessage:" Swagger 2.0不支持:使用路径'api/Credential'和方法'GET'的多项操作.请参阅配置设置-\"ResolveConflictingActions \"以获取可能的解决方法," ExceptionType:" System.NotSupportedException," StackTrace:"位于Swashbuckle.Swagger.SwaggerGeneratorOptions.DefaultConflictingActionsResolver(IEnumerable
1 apiDescriptions)\r\n at Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable
1 apiDescriptions,SchemaRegistry schemaRegistry) \ r \ n,位于Swashbuckle.Swagger.SwaggerGenerator.<> c__DisplayClass7.b__4(IGrouping2 group)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1源,Func2 keySelector, Func
2 elementSelector,IEqualityComparer1 comparer)\r\n at Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)\r\n at Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.HttpServer.
d__0.MoveNext())
An error has occurred.","ExceptionMessage":"Not supported by Swagger 2.0: Multiple operations with path 'api/Credential' and method 'GET'. See the config setting - \"ResolveConflictingActions\" for a potential workaround","ExceptionType":"System.NotSupportedException","StackTrace":" at Swashbuckle.Swagger.SwaggerGeneratorOptions.DefaultConflictingActionsResolver(IEnumerable
1 apiDescriptions)\r\n at Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable
1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n at Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.b__4(IGrouping2 group)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
1 source, Func2 keySelector, Func
2 elementSelector, IEqualityComparer1 comparer)\r\n at Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)\r\n at Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.HttpServer.
d__0.MoveNext()"} http://localhost:50950/swagger/docs/v1
在第二个控制器中,我添加了以下两种方法.
In the second controller I have added following two methods.
string Get(string username, string password);
string Get(string credential);
如果我评论其中一种方法.然后就可以了.
If I comment one of the method. Then it works fine.
任何想法如何解决?
推荐答案
在文件 AppStart/SwaggerConfig.cs
第一个,您必须导入Linq
using System.Linq;
然后在同一行中添加同一行
And add in the same file, this line
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
就在其中:
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{ ...
一个考虑因素:
在控制器中,必须使用Http methods
:
One consideration:
In your controllers, you must use the Http methods
:
[HttpGet]
[Route("something")]
public List<model> something(){....}
[HttpGet]
[Route("something2")]
public List<model2> something2(){....}
[HttpPost]
[Route("mypost1")]
public List<model> mypost1(){....}
[HttpPost]
[Route("mypost2")]
public List<model2> mypost2(){....}
这篇关于Swagger 2.0不支持:带有路径的多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!