Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebApi Swagger: API Endpoints empty list

Problem: Swagger runs fine in my .NET WebAPI project, but no API endpoint is listed.

enter image description here

This is what i get from: http://localhost:62536/swagger/docs/v1

{
   "swagger":"2.0",
   "info":{
      "version":"v1",
      "title":"Backend.WebApi"
   },
   "host":"localhost:62536",
   "schemes":[
      "http"
   ],
   "paths":{

   },
   "definitions":{

   }
}

swaggerConfig.cs

using System.Web.Http;
using WebActivatorEx;
using Backend.WebApi;
using Swashbuckle.Application;

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

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

            GlobalConfiguration.Configuration
                .EnableSwagger(c =>
                    {


                        c.SingleApiVersion("v1", "Backend.WebApi");

                    })
                .EnableSwaggerUi(c =>

                    });
        }
    }
}

Here's a controller example:

testController.cs

 namespace Backend.WebApi.Controllers
    {


    [Authorize]
    [RoutePrefix("api/test")]
    public class TestController : ApiController
    {
        private readonly IAppService _appService;

        public TestController(IAppService appService)
        {
            _appService = appService;
        }

        [Route("vehicles/all")]
        public List<VehicleViewModel> GetVehicles()
        {
            return _appService.GetVehicles();
        }
[...]

I tried enabling XML comment too (since my app has some of them), but nothing pops up on the list. Am i missing something? Thanks.

UPDATE:

  • First attempt: calling swagger register from WebApiConfig.cs -> NOT WORKING

webConfigApi.cs

 public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
                AutoMapperConfiguration.Configure();

                SwaggerConfig.Register();
                ConfigureIoC(config);


                // Web API routes
                config.MapHttpAttributeRoutes();

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

                var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
                jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }

swaggerConfig.cs

// [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

namespace Backend.WebApi
like image 456
Dyd666 Avatar asked May 18 '18 13:05

Dyd666


1 Answers

Moving the config part from SwaggerConfig.cs to WebApiConfig.cs fixed the issue, probably due to the fact we're using OWIN on our project.

webApiConfig.cs

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            [...]
            config
            .EnableSwagger(c => c.SingleApiVersion("v1", "Backend.WebApi"))
            .EnableSwaggerUi();
like image 138
Dyd666 Avatar answered Oct 19 '22 04:10

Dyd666