asp.net-web-api2swaggerstructuremapswagger-uiswashbuckle

swagger UI is not showing anything in webapi


I followed this up to the xml doc part in order to create Swagger documentation using Swashbuckle. It should allow me to view the endpoints via (in my case):

http://localhost:51854/swagger/ui/index

Unfortunately, I cannot see any endpoints:

enter image description here

Any ideas why and how to fix this? Please note that I created my webapi from an empty webapi project - maybe that's the problem. Something must be missing but I am not sure what ...

I have now identified the following code as the root cause. In Global.asax.cs:

var container = new XyzWebApiStructureMapContainerConfigurator().Configure(GlobalConfiguration.Configuration);
GlobalConfiguration.Configuration.Services
.Replace(typeof(IHttpControllerActivator),
new StructureMapHttpControllerActivator(container));

Some classes:

public class XyzWebApiStructureMapContainerConfigurator
{
    public IContainer Configure(HttpConfiguration config)
    {
        var container = new Container(new BlaWebApiRegistry());
        config.DependencyResolver = new StructureMapDependencyResolver(container);
        return container;
    }
}

public class StructureMapDependencyResolver : StructureMapDependencyScope, IDependencyResolver, IHttpControllerActivator
{
    private readonly IContainer _container;

    public StructureMapDependencyResolver(IContainer container)
        : base(container)
    {
        _container = container;
        container.Inject<IHttpControllerActivator>(this);
    }

    public IDependencyScope BeginScope()
    {
        return new StructureMapDependencyScope(_container.GetNestedContainer());
    }

    public IHttpController Create(
        HttpRequestMessage request,
        HttpControllerDescriptor controllerDescriptor,
        Type controllerType)
    {
        var scope = request.GetDependencyScope();
        return scope.GetService(controllerType) as IHttpController;
    }
}

PS:

Simplified controller code:

[RoutePrefix("api/XYZ")]
public class BlaController : ApiController
{
    private readonly ISomething _something;

    public BlaController(ISomething something)
    {
        _something = something;
    }

    [Route("")]
    [HttpGet]
    public IHttpActionResult Resources([FromUri] BlaRequest blaRequest)
    {
        // something exciting
        return Ok(returnObject);
    }
}

PPS:

More code:

// WebApiConfig

public static void Register(HttpConfiguration config)
{
    // Web API configuration and services  

    // Web API routes
    config.MapHttpAttributeRoutes();

    //var cors = new EnableCorsAttribute("*", "*", "*");
    config.EnableCors();

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

// Global.asax.cs

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);

        GlobalConfiguration.Configuration.Formatters.Clear();
        GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());

        var container = new XyzWebApiStructureMapContainerConfigurator().Configure(GlobalConfiguration.Configuration);
        GlobalConfiguration.Configuration.Services
        .Replace(typeof(IHttpControllerActivator),
            new StructureMapHttpControllerActivator(container));
    }
}

PPPS:

{
swagger: "2.0",
info: {
version: "v1",
title: "Bla.Di.Bla"
},
host: "localhost:51854",
schemes: [
"http"
],
paths: { },
definitions: { }
}

Solution

  • In my case the issue was caused by IIS Express Virtual Directory, here is the detailed explanation:

    I have an API project, this is how the project properties looks like:

    enter image description here

    These properties are written to applicationhost.config file, located at: MySolutionPath\.vs\MySolution\config

    This is the section of the file which stores the Virtual Directory information:

    <sites>
      <site name="MyProject" id="3">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
          <virtualDirectory path="/" physicalPath="C:\MyProjectPath\MyProject" />
        </application>
        <bindings>
          <binding protocol="https" bindingInformation="*:44334:localhost" />
          <binding protocol="http" bindingInformation="*:53242:localhost" />
        </bindings>
      </site>
      ..
    </sites>
    

    Everything was working fine...

    Then I started following this guide, which asked me to change the Project URL to: https://localhost:44334/Swagger

    enter image description here

    I did so and then clicked on Create Virtual Directory button.

    Doing so made the following change to applicationhost.config file:

    <sites>
      <site name="MyProject" id="3">
        <application path="/" applicationPool="Clr4IntegratedAppPool">
          <virtualDirectory path="/" physicalPath="C:\MyProjectPath\MyProject" />
        </application>
        <application path="/Swagger" applicationPool="Clr4IntegratedAppPool">
          <virtualDirectory path="/" physicalPath="C:\Shopless\shopless.source\Shopless.Api" />
        </application>
        <bindings>
          <binding protocol="https" bindingInformation="*:44334:localhost" />
          <binding protocol="http" bindingInformation="*:53242:localhost" />
        </bindings>
      </site>
      ..
    </sites>
    

    And that was the issue, because now the path /Swagger points to the root of my project.

    To fix the issue, I had to delete the new section from applicationhost.config file and also and change the Project Url back to it's original value.