Web Api - Swagger documentation error 500

2019-04-22 07:27发布

When I access to the swagger url: http://localhost:28483/swagger/ui/index, it generates this error:

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

Any ideas?

UPDATED: See this detail error in 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

6条回答
走好不送
2楼-- · 2019-04-22 07:42

Have you tried enable this in you swagger config?

c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
查看更多
干净又极端
3楼-- · 2019-04-22 07:49

In the controller, it got two different GET operations and it is disallowed by Swagger. I suggest to either have only single GET operation for each controller or modify the router in WebApiConfig

查看更多
Ridiculous、
4楼-- · 2019-04-22 07:50

I had the same issue when mixing attribute routing with default routes. When I removed the default route the problem went away. The downside is, without the default route defined, I had to add attribute routing to all my controllers.

So from my WebApiConfig I removed:

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

And added attribute routing to my controller:

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

In reality I use a [RoutePrefix("Session")] on my Controller and use [Route("")] on my methods, but the result should be the same.

查看更多
等我变得足够好
5楼-- · 2019-04-22 07:54
config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
查看更多
爷的心禁止访问
6楼-- · 2019-04-22 07:57

Swagger might be considering two actions as one operation (like in the case of this common scenario)...

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

It seems you can use attribute routing to fix this and use these attributes above your actions so swagger will recognize them separately.

[Route("api/Products")]

[Route("api/Products/{id:guid}")]
查看更多
干净又极端
7楼-- · 2019-04-22 08:02

I was getting this error due to the parameter names not matching between the Attribute Routing statement and the method signature.

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

After changing "{id}" to "{deviceNumber}" it fixed the error.

查看更多
登录 后发表回答