简体   繁体   English

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

[英]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.我跟着这个到XML文档的一部分,以便使用Swashbuckle创建扬鞭文档。 It should allow me to view the endpoints via (in my case):它应该允许我通过(在我的情况下)查看端点:

http://localhost:51854/swagger/ui/index 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.请注意,我从一个空的 webapi 项目创建了我的 webapi - 也许这就是问题所在。 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:在 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: 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 // 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 // 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.在问题的类 XyzWebApiStructureMapContainerConfigurator 中引起了一些问题。

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).结果证明 DI(在我的例子中是 Unity)被配置为绑定所有加载的程序集(其中之一是 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.我在 swagger 时遇到了同样的问题,并花了很长时间来寻找解决方案。 Finally got one.终于拿到了一张。 Add [HttpGet] or [HttpPost] attribute to each of controller's action method.将 [HttpGet] 或 [HttpPost] 属性添加到每个控制器的操作方法。

[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.该问题是由 Structuremap 扫描引起的。 I had the following lines of code in my Structuremap.Registry class我的 Structuremap.Registry 类中有以下几行代码

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

The line线

scan.AssembliesFromApplicationBaseDirectory(); scan.AssembliesFromApplicationBaseDirectory();

was stopping Swagger working.正在阻止 Swagger 工作。 I've changed it to我已将其更改为

scan.AssembliesFromApplicationBaseDirectory(MyDllsPathFilter());

and now Swagger is working as expected.现在 Swagger 按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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