4

In my ASP.NET Web Api (ASP.NET 4 MVC 5) application I have two routes configured like so:

        // Default route. Comes "out of the box" with Web Api.
        // e.g. api/users/123
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // A route for child controllers. See for example the LockController 
        // that is "nested" under the "UsersController".
        // e.g. api/users/123/lock
        config.Routes.MapHttpRoute(
            name: "ChildApi",
            routeTemplate: "api/{parentController}/{parentId}/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

These actually work quite well in that I have a "top level" UsersController that is matched by the first route and a "child" LockController (in a Users folder) that is matched by the second route.

Now that I've added Swashbuckle 5 (Swagger for .net) to my application, Swagger is confused into showing both routes for each method I've implemented. For example:

enter image description here

So Swagger recognizes the LockController as both a top-level controller and a child controller.

How can I tell Swagger that a particular controller is top-level or child?

Additionally, can I make swagger recognize that the path to the lock resource, for example, is /api/users/{id}/lock and not /api/{parentController}/{parentId}/lock as Swagger is currently showing?

urig
  • 16,016
  • 26
  • 115
  • 184
  • http://stackoverflow.com/questions/10783946/how-to-handle-hierarchical-routes-in-asp-net-web-api – Amit Kumar Ghosh Jan 05 '16 at 13:21
  • 1
    Thanks. That's exactly where I got my routes from. Thing is, I want to keep using these generic routes, rather than specific routes that explicitly mention the parent controller name. I'm aiming for convention over configuration. – urig Jan 05 '16 at 13:30

2 Answers2

1

I think problem is not related with inheritance hierarchy. Swashbuckle generates routes for every route mapping. To prevent Swashbuckle to generate duplicate routes , one solution could be adding controller constraint to routes.I hope this helps.

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

          config.Routes.MapHttpRoute(
            name: "ChildApi",
            routeTemplate: "api/{parentController}/{parentId}/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { controller = "Lock" }

        );
Mustafa ASAN
  • 3,747
  • 2
  • 23
  • 34
-1

I think this should work -

config.Routes.MapHttpRoute("childapi", 
     "api/{Parent}/{i}/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });

Works fine for -

http://localhost:60953/api/Another/1/Child/GetFlag/poo

http://localhost:60953/api/Parent/1/Child/GetFlag/poo

http://localhost:60953/api/Child/1/Another/GetString/test
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24
  • This obviously does not work. There's no principle difference from the route I mentioned in the question. Swagger is still showing duplicate methods. – urig Jan 06 '16 at 07:58