Search code examples
asp.net-mvc-routingasp.net-web-apiasp.net-web-api-helppages

Web API routing and a Web API Help Page: how to avoid repeated entries


I am getting repeat entries rendered in my Web API Help Page with different parents, such as these, that refer to the same method:

GET api/{apiVersion}/v1/Products - Gets all products

...

GET api/v1/Products - Gets all products

...

I have a Web API page with some routing like this:

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

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

I had thought that this routing would make the "v1" optional, so the derived documentation above is not expected.

(sidebar: Going to api/products certainly doesn't work, so I am not sure what is wrong with this. What am I missing?)

It seems the real problem is that Web API Help Page is reading the routes improperly, as I thought v1 and {apiVersion} should not both appear in the same action. What am I missing here?


Solution

  • It seems like this is a shortcoming of the ASP.NET Web API help pages. To workaround, I changed the view to exclude these invalid routes from the rendered document. For the above example, I added this Where clause to the loop in ApiGroup.cshtml, changing

    @foreach (var api in Model){
    

    to

    @foreach (var api in Model.Where(m => !m.Route.RouteTemplate.Contains(@"{apiVersion}"))){