![](/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.