Swagger 2.0不支持:带路径的多个操作
我在WebApi 2应用程序中集成了swagger。 应用程序有单个控制器时,它工作正常。 当我在应用程序中添加第二个控制器时。 我收到了以下错误:
发生错误。“,”ExceptionMessage“:”Swagger 2.0不支持:路径’api / Credential’和方法’GET’的多个操作。 请参阅配置设置 – \“ResolveConflictingActions \”以获取潜在的解决方法“,”ExceptionType“:”System.NotSupportedException“,”StackTrace“:”在Swashbuckle的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 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.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.
2 keySelector, Func
1 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.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.
1 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.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
在第二个控制器中,我添加了以下两种方法。
string Get(string username, string password); string Get(string credential);
如果我评论其中一种方法。 然后它工作正常。
任何想法如何解决它?
在AppStart / SwaggerConfig.cs文件中
首先,你必须导入Linq
using System.Linq;
并在同一个文件中添加此行
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
就在里面:
GlobalConfiguration.Configuration .EnableSwagger(c => { ...
一个考虑因素:在控制器中,必须使用Http methods
:
[HttpGet] [Route("something")] public List something(){....} [HttpGet] [Route("something2")] public List something2(){....} [HttpPost] [Route("mypost1")] public List mypost1(){....} [HttpPost] [Route("mypost2")] public List mypost2(){....}
上述两种方法有两种选择:
-
将两个方法合并为一个具有三个参数的方法 – 每个方法都在查询字符串中
-
有单独的路由url,如 –
api/controller/byusername
和api/controller/bycredentials
在使用属性路由的情况下也会出现此问题。
如果属性路由与可路由路由冲突,则会出现“多个操作”错误。
例:
[HttpGet] [SwaggerOperation("GetByUsername")] [Route("[path]/User")] public IHttpActionResult GetUser(string username) { }
有关属性路由的更多信息: https : //docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
为方法[Route("ApiAnotherFunction")]
和[HttpGet]
添加路由属性。
阅读文档怎么样? 它说Swashbuckle不支持这种方法签名。 但是,您可以创建一个操作filter来设置id或使用本SwaggerOperationAttribute
中指出的SwaggerOperationAttribute
。