Swagger 2.0 不支持:路径 'api/Client' 和方法 'GET' 的多个操作
Not supported by Swagger 2.0: Multiple operations with path 'api/Client' and method 'GET'
在我的网站 api 中,我有 2 种获取方法,一种用于获取所有客户端,一种用于通过 ID 获取客户端
[HttpGet]
public async Task<IHttpActionResult> GetClients()
{
var telemetry = new TelemetryClient();
try
{
var roles = await CosmosStoreHolder.Instance.CosmosStoreClient.Query().ToListAsync();
return Ok(roles);
}
catch (System.Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
[HttpGet]
public async Task<IHttpActionResult> GetClient(string clientId)
{
var telemetry = new TelemetryClient();
try
{
var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
var client = await clientStore.Query().FirstOrDefaultAsync(x => x.Id == clientId);
if (client == null)
{
return NotFound();
}
return Ok(client);
}
catch (System.Exception ex)
{
telemetry.TrackException(ex);
return BadRequest("Unknown error");
}
}
我刚刚安装了swashbuckle并按照这个配置:
https://www.c-sharpcorner.com/article/implementing-swagger-in-web-api/
但是我得到以下错误
500 : {"Message":"An error has occurred.","ExceptionMessage":"Not
supported by Swagger 2.0: Multiple operations with path 'api/Client'
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(IEnumerable1
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](IEnumerable1
source, Func
2 keySelector, Func2 elementSelector,
IEqualityComparer
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.HttpServer.d__0.MoveNext()"}
https://webapi-app.azurewebsites.net/swagger/do`cs/v1
您的问题是由字符串参数引起的,字符串默认可为空。
您的行动路线:
GetClients()
GetClient(string clientId)
会一样。
如果它是一个整数,路线将如下所示:
/api/Controller/{clientId}
并且不会与 GetClients 冲突。
更改数据类型可能不是一个选项,您可能被迫使用路由:
查看我的一些示例,我发现这个控制器使用路由和多个 Gets:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Location#/
您可以看到我在同一个控制器下有多个 get,但它们的操作确实略有不同:
[RoutePrefix("Location")]
public class LocationController : ApiController
{
[Route("Get1")]
public GeolocateResponse Get1([FromUri] GeolocateResponse x)
{
return x;
}
[Route("Get2")]
public Location Get2([FromUri] Location x)
{
return x;
}
在我的网站 api 中,我有 2 种获取方法,一种用于获取所有客户端,一种用于通过 ID 获取客户端
[HttpGet]
public async Task<IHttpActionResult> GetClients()
{
var telemetry = new TelemetryClient();
try
{
var roles = await CosmosStoreHolder.Instance.CosmosStoreClient.Query().ToListAsync();
return Ok(roles);
}
catch (System.Exception ex)
{
string guid = Guid.NewGuid().ToString();
var dt = new Dictionary<string, string>
{
{ "Error Lulo: ", guid }
};
telemetry.TrackException(ex, dt);
return BadRequest("Error Lulo: " + guid);
}
}
[HttpGet]
public async Task<IHttpActionResult> GetClient(string clientId)
{
var telemetry = new TelemetryClient();
try
{
var clientStore = CosmosStoreHolder.Instance.CosmosStoreClient;
var client = await clientStore.Query().FirstOrDefaultAsync(x => x.Id == clientId);
if (client == null)
{
return NotFound();
}
return Ok(client);
}
catch (System.Exception ex)
{
telemetry.TrackException(ex);
return BadRequest("Unknown error");
}
}
我刚刚安装了swashbuckle并按照这个配置:
https://www.c-sharpcorner.com/article/implementing-swagger-in-web-api/
但是我得到以下错误
500 : {"Message":"An error has occurred.","ExceptionMessage":"Not supported by Swagger 2.0: Multiple operations with path 'api/Client' 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(IEnumerable1 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](IEnumerable1 source, Func
2 keySelector, Func2 elementSelector, IEqualityComparer
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.HttpServer.d__0.MoveNext()"} https://webapi-app.azurewebsites.net/swagger/do`cs/v1
您的问题是由字符串参数引起的,字符串默认可为空。
您的行动路线:
GetClients()
GetClient(string clientId)
会一样。
如果它是一个整数,路线将如下所示:
/api/Controller/{clientId}
并且不会与 GetClients 冲突。
更改数据类型可能不是一个选项,您可能被迫使用路由:
查看我的一些示例,我发现这个控制器使用路由和多个 Gets:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Location#/
您可以看到我在同一个控制器下有多个 get,但它们的操作确实略有不同:
[RoutePrefix("Location")]
public class LocationController : ApiController
{
[Route("Get1")]
public GeolocateResponse Get1([FromUri] GeolocateResponse x)
{
return x;
}
[Route("Get2")]
public Location Get2([FromUri] Location x)
{
return x;
}