Problem: Swagger runs fine in my .NET WebAPI project, but no API endpoint is listed.
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:
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
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With