-->

Not supported by Swagger 2.0: Multiple operations

2019-03-24 17:22发布

问题:

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 :

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)\r\n at Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable1 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](IEnumerable1 source, Func2 keySelector, Func2 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.

Any Idea how to fix it?

回答1:

In the file AppStart/SwaggerConfig.cs

First one, you must import Linq

using System.Linq;

And add in the same file, this line

c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());

just inside of:

GlobalConfiguration.Configuration 
                .EnableSwagger(c =>
                    { ...

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(){....}


回答2:

You have two options for the above two methods:

  1. Combine two methods into single one with three parameters - each one will be in the query string

  2. Have separate route urls like - api/controller/byusername and api/controller/bycredentials



回答3:

The problem can also occur in a situation where you use attribute routing.

If the attribute route conflicts with a routingtable route you will have the 'multiple operations' error.

Example:

[HttpGet]
[SwaggerOperation("GetByUsername")]
[Route("[path]/User")]
public IHttpActionResult GetUser(string username)
{

}

more info on attribute routing: https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2



回答4:

Add seperate route on your conflicting methods. Such as [Route("GetByType")] above of one and [Route("GetById")] on another



回答5:

Add route attribute for the method [Route("ApiAnotherFunction")] and [HttpGet].



回答6:

How about reading the docs? It says Swashbuckle does not support this kind of method signatures. You can however create an operation filter to set the id or use the SwaggerOperationAttribute as pointed out in this Q&A.