4

I've been trying to figure out why Swagger-Net does not show the endpoint methods in a controller.

How it looks when Swagger references only the controller

The endpoint method looks like this

The C# project is using a Web API template based on .Net framework 4.6.1.

I get the same result when I use SwashBuckler, so it's not Swagger-Net that's the issue, but something that is not configured or missing.

The SwaggerConfig looks like this

    public class SwaggerConfig
{
    /// <summary>
    /// 
    /// </summary>
    public static void Register()
    {
        var thisAssembly = typeof(SwaggerConfig).Assembly;

        GlobalConfiguration.Configuration
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", nameof(ConsentResponseApp));

                    c.AccessControlAllowOrigin("*");

                  c.IncludeAllXmlComments(thisAssembly, AppDomain.CurrentDomain.BaseDirectory);

                })
            .EnableSwaggerUi(c =>
                {
                 c.UImaxDisplayedTags(100);

                    c.UIfilter("''");
                    });
    }
  }

I'm at a dead end at the moment since I have no idea why Swagger cannot read the methods action names.

The answer:

The WebApiConfig route is not by default configured to route with action

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

it has to be changed to

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

With the {action} parameter added

  • Have you enabled XML documentation in your web api project's properties? – ethane Nov 30 '17 at 15:07
  • I think you need a route, not actionname; besides that it is advised you also add your responses. : [SwaggerResponse(HttpStatusCode.OK, "OK", typeof(User))] [SwaggerResponse(HttpStatusCode.BadRequest, "Error", typeof(string))] [SwaggerResponse(HttpStatusCode.NotFound, "Notfound", typeof(string))] [Route("SaveConsent")] [HttpPost] – Leon Nov 30 '17 at 15:13
  • Changing the route was the key. The route looked like this at WebApiConfig.cs config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); Needed to change it to config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); Where the {action} was added between the controller and id – Harald S. Hanssen Dec 01 '17 at 09:20
  • @leon Put an answer so I can give you the point. – Harald S. Hanssen Dec 01 '17 at 11:11

2 Answers2

2

Building on Leon's comment. You need to specify a Route as Leon showed above.

I'm not sure [ActionName()] is what you need at all since it will allow your API's consumer to specify the URI with characters .NET may not allow or using a different signature than your actual controller method.

See this post for the reason behind [ActionName()].

ethane
  • 2,329
  • 3
  • 22
  • 33
1

You need a route, not an actionname

[Route("SaveConsent")] 

Besides that, it is advisable to add expected responses like so:

[SwaggerResponse(HttpStatusCode.OK, "OK", typeof(User))] [SwaggerResponse(HttpStatusCode.BadRequest, "Error", typeof(string))] [SwaggerResponse(HttpStatusCode.NotFound, "Notfound", typeof(string))] 
[Route("SaveConsent")] 
[HttpPost] 
Leon
  • 919
  • 6
  • 21