When working with built in swagger ui of Api Apps in Visual Studio, you might get following error, when you navigate to swagger URL: http://localhost:9236/swagger/docs/v1.
500 : undefined http://localhost:9236/swagger/docs/v1
If you navigate back an the exception stack frame in Visual Studio, you will find this:
{"Message":"An error has occurred.","ExceptionMessage":"Not supported by Swagger 2.0: Multiple operations with path 'api/LoadUnits' 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.<GetSwagger>b__4(IGrouping`2 group)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)\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.HttpServer.<>n__FabricatedMethod9(HttpRequestMessage , CancellationToken )\r\n at System.Web.Http.HttpServer.<SendAsync>d__0.MoveNext()"}
My controller has two methods which start with the same word ‘Get’. This seems to make swagger generatot crazy.
public LoadUnit GetById(long id)
{
return new LoadUnit() { Date = DateTime.Now, };
}
public LoadUnit GetByExternalId(string externalId, DateTime atTime)
{
return new LoadUnit() { Date = atTime, ExternalId = externalId, Id = 1 };
}
To workaround this, please rename one of methods.
public LoadUnit AGetByExternalId(string externalId, DateTime atTime)
{
return new LoadUnit() { Date = atTime, ExternalId = externalId, Id = 1 };
}
Posted
Apr 14 2015, 07:10 AM
by
Damir Dobric