Swagger 2.0 不支持:带路径的多个操作

Not supported by Swagger 2.0: Multiple operations with path(Swagger 2.0 不支持:带路径的多个操作)
本文介绍了Swagger 2.0 不支持:带路径的多个操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WebApi 2 应用程序中集成了 swagger.当应用程序具有单个控制器时,它可以正常工作.当我在应用程序中添加第二个控制器时.我收到以下错误:

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(IEnumerable1 apiDescriptions) 在 Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable1 apiDescriptions, SchemaRegistry schemaRegistry) 在 Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.b__4(IGrouping2 组) 在 System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 源,Func2 keySelector,Func2 elementSelector,IEqualityComparer1 比较器) 在 Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(字符串 rootUrl,字符串 apiVersion) 在 Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage 请求,CancellationToken cancelToken) 在 System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage请求,CancellationToken 取消令牌) 在 System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancelToken) 在 System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancelToken) 在 System.Web.Http.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext() --- 在 System.Runtime.CompilerServices.TaskAwaiter 上一个引发异常的位置结束堆栈跟踪 --- .ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.HttpServer.d__0.MoveNext()"} http://localhost:50950/swagger/docs/v1

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(IEnumerable1 apiDescriptions) at Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable1 apiDescriptions, SchemaRegistry schemaRegistry) at Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.b__4(IGrouping2 group) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion) at Swashbuckle.Application.SwaggerDocsHandler.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.Cors.CorsMessageHandler.<SendAsync>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 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 方法:

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 不支持:带路径的多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)