简体   繁体   中英

Error enabling swagger with swashbucle

I am trying to create a WebApi project with a couple of simple controllers. If I call the methods using fiddler all is fine, however I prefer to use swashbuckle as it's a bit prettier.

However, with swashbuckle installed using the default configuration it isn't working.

When I navigate to http://localhost/api/mycontroller/swagger

it redirects to http://localhost/api/mycontroller/swagger/ui/index

But then it just displays the following error:

<Error>
<Message>
No HTTP resource was found that matches the request URI ' http://localhost/api/Management/swagger/ui/index '.
</Message>
<MessageDetail>
No type was found that matches the controller named 'swagger'. </MessageDetail>
</Error>

My routing is as follows: config.MapHttpAttributeRoutes();

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

We found a very simple solution to our issue.

When swagger was added to the project it automatically updated the Bootstrapper.cs within App_Start, and it looked like this:

    WebApiConfig.Register(config);
    UnityConfig.Register(config, container);
    SignalRConfig.Register(app, container);
    SwaggerConfig.Register(config);

Simply moving swagger to the top of the list allowed it to register it's routes correctly before the web api routes:

    SwaggerConfig.Register(config); // Swagger must be the first thing in this sequence or it just does not work...
    WebApiConfig.Register(config);
    UnityConfig.Register(config, container);
    SignalRConfig.Register(app, container);

Just posting this in the hope that it helps someone else.

I had the same issue, we are only using attribute routing so this may not apply to everyone.

By default the WebApiConfig looks like this:

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

If you remove the section for to MapHttpRoute for the default routing and just have:

config.MapHttpAttributeRoutes();

It works like a champ!!! I tried following the FAQ regarding fixing it but this was the only thing that worked. Also another item to consider is that I only needed Swashbuckle.Core.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM