Cannot see my controllers actions in the Swagger UI

6.3k views Asked by At

I am trying to setup my web api project to use Swagger.

I installed Swashbuckle and while the Swagger UI works when I go to http://localhost:55010/swagger, I see none of my controllers actions.

enter image description here

I am using this kind of path for my actions: http://localhost:55010/api/v1/Role

I currently have only one version of my api, but I am planning to have more than one so I am using v1 in my URL paths (it is set up by using subfolders in my Controllers folder).

Here is what I see when I go to http://localhost:55010/swagger/docs/v1:

{"swagger":"2.0","info":{"version":"v1","title":"Swashbuckle Dummy API V1"},"host":"localhost:55010","schemes":["http"],"paths":{},"definitions":{}}

This is the configuration that I am using:

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

        GlobalConfiguration.Configuration 
            .EnableSwagger(c =>
                {
                    c.MultipleApiVersions(
                        (apiDesc, targetApiVersion) => ResolveVersionSupportByRouteConstraint(apiDesc, targetApiVersion),
                        (vc) =>
                        {
                            //vc.Version("v2", "Swashbuckle Dummy API V2");
                            vc.Version("v1", "Swashbuckle Dummy API V1");
                        });
                })
            .EnableSwaggerUi(c =>
                {
                });
    }

    private static bool ResolveVersionSupportByRouteConstraint(ApiDescription apiDesc, string targetApiVersion)
    {
        // I don't know how I am supposed to use this
        return true;
    }
}

My Route config :

config.Routes.MapHttpRoute(
    name: "WithActionApi",
    routeTemplate: "api/{folder}/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional },
    constraints: new { action = @"[A-Za-z]+" }
);

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

and one example of a controller :

public class GridTemplateController : BaseController
{
    GridTemplateLogic logic;

    public GridTemplateController(IPermissionValidator permissionValidator, IRolePermissionLogic logicRolePermission)
        : base(permissionValidator, logicRolePermission)
    {
        logic = new GridTemplateLogic(new GridTemplateRepository(ConnectionString, CurrentUser), permissionValidator);
    }

    // GET: api/v1/GridTemplate/ForGrid/5
    [HttpGet]
    public IHttpActionResult ForGrid(int id)
    {
        try
        {
            var entityList = logic.GetAllByGridId(id);
            return Ok(new ApiResponse(entityList));
        }
        catch (UnauthorizedAccessException)
        {
            return Unauthorized();
        }
    }

    ...........
2

There are 2 answers

1
sibvic On

Change swagger configuration to this:

public class SwaggerConfig
{
    public static void Register(HttpConfiguration config)
    ...
}

And configure it after all the other configurations:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ...
        SwaggerConfig.Register(config);
    }
1
Florian SANTI On

I removed the [AcceptVerbs("xxx")] on my methods and they appeared in my Swagger :-)