![](/img/trans.png)
[英]Not supported by Swagger 2.0: Multiple operations with path 'api/Client' and method 'GET'
[英]Not supported by Swagger 2.0: Multiple operations with path
我在 WebApi 2 应用程序中集成了 swagger。 当应用程序具有单个控制器时,它工作正常。 当我在应用程序中添加第二个控制器时。 我收到以下错误:
发生错误。","ExceptionMessage":"Swagger 2.0 不支持:具有路径“api/Credential”和方法“GET”的多重操作。 请参阅配置设置 - \\"ResolveConflictingActions\\" 以获取潜在的解决方法","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 在 Swashbuckle.Swagger.SwaggerGenerator.<>c__DisplayClass7.b__4(IGrouping2 group)\\r\\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable
ToDictionary2 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.<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.
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.<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.
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.<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
在第二个控制器中,我添加了以下两种方法。
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<model> something(){....}
[HttpGet]
[Route("something2")]
public List<model2> something2(){....}
[HttpPost]
[Route("mypost1")]
public List<model> mypost1(){....}
[HttpPost]
[Route("mypost2")]
public List<model2> mypost2(){....}
对于上述两种方法,您有两种选择:
将两种方法合并为一个,具有三个参数 - 每个方法都将在查询字符串中
有单独的路由 url,如 - api/controller/byusername
和api/controller/bycredentials
我知道这是一个旧线程。 我稍微偏离了 OP 问题。 但是,我最近遇到了这个问题,并花了很长时间试图弄清楚发生了什么(此页面是该问题的 google 顶部)。 它可以帮助其他人免于经历数小时的痛苦。
我的控制器都扩展了一个基本控制器,我愚蠢地拥有一个公共功能而不是受保护的功能。 因此,swagger 将公共职能视为重复。
希望这可以帮助其他人避免花费不必要的时间。
您可以更改控制器上的路线。
默认值为[Route("api/[controller]")]
你可以改成[Route("api/[controller]/[action]")]
我还没有看到的答案:我搞砸了用途。
我的控制器有一些 MVC 项目(列表项目),因此有一个链接:
using System.Web.Http;
using System.Web.Mvc;
因此,不知何故,它注册了两次(我假设)。 问题已通过使用 RoutePrefix/Route/HttpGet 等的完整限定条件得到解决。
namespace MyNameSpace
{
[System.Web.Http.RoutePrefix("api/Reports")]
public class ReportsController
{
{...constructor...}
[System.Web.Http.Route("Scans")]
[System.Web.Http.HttpGet,ResponseType(typeof(List<ReportClass>))]
public ascyn Task<HttpResponseMessage> GetScans() {...}
在使用属性路由的情况下也会出现此问题。
如果属性路由与可路由的路由冲突,您将遇到“多重操作”错误。
例子:
[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("GetByType")]
上方的一个的和[Route("GetById")]
上的另一
抱歉,我回答得太晚了:问题的解决方案是,如果您有多个控制器,则删除控制器上的任何路由版本控制属性; 默认情况下,swagger 从 WebApiConfig 中选择路由。 注意:您不需要提供操作名称或路由。 只要确保没有两个动作具有相同的名称或签名; 当然,它永远无法编译。
为[Route("ApiAnotherFunction")]
和[HttpGet]
方法添加路由属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.