Web API - Swagger文档错误500

ma8fv8wu  于 11个月前  发布在  其他
关注(0)|答案(7)|浏览(271)

当我访问swagger url:http://localhost:28483/swagger/ui/index时,它会生成以下错误:

500 : undefined http://localhost:28483/swagger/docs/v1

字符串
有什么想法吗?
错误:请参阅firebug中的此详细错误:

Not supported by Swagger 2.0: Multiple operations
 with path 'api/BimModel' and method 'GET'. See the config setting - \"ResolveConflictingActions\" for
 a potential workaround

sbtkgmzw

sbtkgmzw1#

Swagger可能会将两个操作视为一个操作(就像这个常见场景中的情况一样)。

GET api/Products
GET api/Products/{id}

字符串
看起来你可以使用attribute routing来解决这个问题,并在你的操作上使用这些属性,这样swagger就可以单独识别它们。

[Route("api/Products")]

[Route("api/Products/{id:guid}")]

x4shl7ld

x4shl7ld2#

你试过在swagger配置中启用这个功能吗?

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

字符串

5ktev3wc

5ktev3wc3#

在控制器中,它有两个不同的GET操作,Swagger不允许。我建议每个控制器只有一个GET操作,或者modify the router in WebApiConfig

kmynzznz

kmynzznz4#

我在混合属性路由和默认路由时遇到了同样的问题。当我删除默认路由时,问题就消失了。缺点是,没有定义默认路由,我必须向所有控制器添加属性路由。
所以从我的WebApiConfig中删除了:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

字符串
并将属性路由添加到我的控制器:

[Route("Session")] // Added this attribute
public async Task<IHttpActionResult> Get()
...    
[Route("Session/{id}")] // Added this attribute
public async Task<IHttpActionResult> Get(int id)


实际上,我在Controller上使用[RoutePrefix("Session")],在方法上使用[Route("")],但结果应该是一样的。

dpiehjr4

dpiehjr45#

我得到这个错误是由于参数名称不匹配之间的属性路由语句和方法签名。

[HttpGet("{id}")]
public IActionResult Get(string deviceNumber){
...

字符串
将“{id}”更改为“{deviceNumber}”后,它修复了错误。

bq8i3lrv

bq8i3lrv6#

在我的例子中,错误是由重复的http属性引起的:

[HttpPost("PostCustomer")]
public async Task<IActionResult> PostCustomer([FromBody] Customer cust)

字符串
我们正在创建一个新版本的端点,所以我复制并粘贴了旧方法,重命名了函数名,忘记重命名属性:

[HttpPost("PostCustomer")] // Duplicate attribute maps to above method
public async Task<IActionResult> CreateNewCustomer([FromBody] Customer2 cust)


当第二个属性被重新命名为新的方法时,斯瓦格又没事了。

v64noz0r

v64noz0r7#

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

字符串

相关问题