Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 }
        );
like image 431
ciantrius Avatar asked May 26 '26 08:05

ciantrius


1 Answers

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.

like image 70
ciantrius Avatar answered May 30 '26 10:05

ciantrius