简体   繁体   中英

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:

在此处输入图片说明

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: { }
}

Turns out this line:

config.DependencyResolver = new StructureMapDependencyResolver(container);

in the question's class XyzWebApiStructureMapContainerConfigurator caused some issues.

Hope this helps someone in the future.

Had the same issue. Turns out the DI (Unity in my case) was configured to bind all loaded assemblies (one of which is Swashbuckle.Core).

Just a bit of refining which assemblies get binded has solved the issue:

var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(asm => asm.FullName.StartsWith("MySolution.MyProject"));

// Web API configuration and services
var container = new UnityContainer();
container.RegisterTypes(
    AllClasses.FromAssemblies(assemblies),
    WithMappings.FromMatchingInterface,
    WithName.Default);

config.DependencyResolver = new UnityDependencyResolver(container);

I faced same issue with swagger and took me long hours to search for solution. Finally got one. Add [HttpGet] or [HttpPost] attribute to each of controller's action method.

[HttpPost]
public IActionResult TestMethod()
 {
  //// Some code
 }

I realise this is an old issue, but I've just encountered it & have a different solution to the ones already suggested. I hope its of use to anyone else getting the problem.

The issue was caused by Structuremap scanning. I had the following lines of code in my Structuremap.Registry class

Scan(scan =>
{
    ....
    scan.AssembliesFromApplicationBaseDirectory();
    scan.TheCallingAssembly();
    scan.WithDefaultConventions();
});

The line

scan.AssembliesFromApplicationBaseDirectory();

was stopping Swagger working. I've changed it to

scan.AssembliesFromApplicationBaseDirectory(MyDllsPathFilter());

and now Swagger is working as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM