swagger UI 在 webapi 中没有显示任何内容

swagger UI is not showing anything in webapi

我一直关注 this 直到 xml 文档部分,以便使用 Swashbuckle 创建 Swagger 文档。它应该允许我通过(在我的例子中)查看端点:

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

很遗憾,我看不到任何端点:

知道为什么以及如何解决这个问题吗?请注意,我是从一个空的 webapi 项目创建我的 webapi - 也许这就是问题所在。一定是少了什么,但我不确定是什么......

我现在已经确定以下代码是根本原因。在 Global.asax.cs:

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

一些类:

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:

简化的控制器代码:

[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:

更多代码:

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

结果是这一行:

config.DependencyResolver = new StructureMapDependencyResolver(container);

问题的 class XyzWebApiStructureMapContainerConfigurator 导致了一些问题。

希望这对以后的人有所帮助。

有同样的问题。事实证明,DI(在我的例子中是 Unity)被配置为绑定所有加载的程序集(其中一个是 Swashbuckle.Core)。

只需稍微改进绑定哪些程序集就解决了这个问题:

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);

我遇到了同样的 swagger 问题,花了很长时间来寻找解决方案。终于拿到了 为每个控制器的操作方法添加 [HttpGet] 或 [HttpPost] 属性。

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

我知道这是一个老问题,但我刚刚遇到它并且对已经提出的问题有不同的解决方案。我希望它对其他遇到问题的人有用。

问题是由结构图扫描引起的。我的 Structuremap.Registry class

中有以下代码行
Scan(scan =>
{
    ....
    scan.AssembliesFromApplicationBaseDirectory();
    scan.TheCallingAssembly();
    scan.WithDefaultConventions();
});

scan.AssembliesFromApplicationBaseDirectory();

正在停止 Swagger 工作。我已将其更改为

scan.AssembliesFromApplicationBaseDirectory(MyDllsPathFilter());

现在 Swagger 正在按预期工作。