我在WebApi 2应用程序中集成了swagger . 应用程序有单个控制器时,它工作正常 . 当我在应用程序中添加第二个控制器时 . 我收到以下错误:
发生错误 . “,”ExceptionMessage“:”Swagger 2.0不支持:路径'api / Credential'和方法'GET'的多个操作 . 请参阅配置设置 - \“ResolveConflictingActions \”以获取潜在的解决方法“,”ExceptionType“:”System.NotSupportedException“,”StackTrace“:”在Swashbuckle的Swashbuckle.Swagger.SwaggerGeneratorOptions.DefaultConflictingActionsResolver(IEnumerable1 apiDescriptions)\ r \ n . Swagger.SwaggerGenerator.CreatePathItem(IEnumerable1 apiDescriptions,SchemaRegistry schemaRegistry)\ r \ n在Swashbuckle.Swagger.SwaggerGenerator . <> c__DisplayClass7.b__4(IGrouping2组)\ r \ n在System.Linq.Enumerable.ToDictionary [TSource,TKey,TElement ](IEnumerable1 source,Func2 keySelector,Func2 elementSelector,IEqualityComparer1 comparer)\ r \ n在Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl,String apiVersion)\ r \ n在Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage请求,CancellationToken)在System.Web.Http.Dispatcher.HttpRoutingDis的System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ r \ n中取消\ n \ n \ n \ n \ n \ n \ n \ n在System.Web.Http.Cors.CorsMessageHandler的System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ r \ n的patcher.SendAsync(HttpRequestMessage请求,CancellationToken cancellationToken)\ r \ n . <SendAsync> d__0 .MoveNext()\ r \ n ---从抛出异常的上一个位置开始的堆栈跟踪结束---在系统中的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n中的\ r \ n . System.Web.Http.HttpServer.d__0.MoveNext()上的Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n“}”} http:// localhost:50950 / swagger / docs / v1
在第二个控制器中,我添加了以下两种方法 .
string Get(string username, string password);
string Get(string credential);
如果我评论其中一种方法 . 然后它工作正常 .
任何想法如何解决它?
6 回答
在文件 AppStart/SwaggerConfig.cs
第一个,你必须导入
Linq
并在同一个文件中添加此行
就在里面:
一个考虑因素:在控制器中,必须使用
Http methods
:上述两种方法有两种选择:
将两个方法合并为一个具有三个参数的方法 - 每个方法都在查询字符串中
有单独的路线网址,如
api/controller/byusername
和api/controller/bycredentials
在使用属性路由的情况下也会出现此问题 .
如果属性路由与可路由路由冲突,则会出现“多个操作”错误 .
例:
有关属性路由的更多信息:https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
在冲突的方法上添加单独的路线 . 如
[Route("GetByType")]
上面的一个和[Route("GetById")]
上的另一个为方法
[Route("ApiAnotherFunction")]
和[HttpGet]
添加路由属性 .reading the docs怎么样?它说Swashbuckle不支持这种方法签名 . 但是,您可以创建一个操作过滤器来设置id或使用
SwaggerOperationAttribute
中指出的SwaggerOperationAttribute
.