【问题标题】:Why, after owin implementation, swagger not work?为什么 owin 实施后,招摇不工作?
【发布时间】:2019-04-25 13:30:26
【问题描述】:

我正在使用 Microsoft Web API 2 和 SwaggerUi 开发一个 RESTful Web 服务。

我已经删除了默认的 Global.asax,并且我已经使用 OWIN 配置了我的 Web API。这是我的 Startup.cs 代码:

    public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();

        WebApiConfig.Register(config);

        config.MessageHandlers.Add(new JWTAuthenticationHandler() { StopPipelineHandling = true });

        config.Filters.Add(new MyActionFilter());

        SwaggerConfig.Register();

        app.UseWebApi(config);

    }

这是我的 SwaggerConfig.cs 代码:

        GlobalConfiguration.Configuration 
            .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "MyNamespace.WebApi");
                })
            .EnableSwaggerUi(c =>
                {});

执行此操作后,我无法像以下图片一样查看 swagger 上的操作:

拜托,你能帮帮我吗?

非常感谢

【问题讨论】:

  • 我建议您使用浏览器开发工具 (F12) 查看从您的 Web API 返回的内容。您很可能会收到错误状态代码,然后必须修复。
  • 嗨 Martin,我已经尝试过了,我可以正确使用我的 API。
  • 试试这个而不是使用“GlobalConfiguration.Configuration”将httpConfiguration传递给swagger寄存器。公共静态无效寄存器(HttpConfiguration config){ config.EnableSwagger(c => { c.SingleApiVersion(“v1”,“MyNamespace.WebApi”);}).EnableSwaggerUi();}

标签: c# asp.net-web-api swagger swagger-ui


【解决方案1】:

我已经解决了改变:

Startup.cs

public void Configuration(IAppBuilder app)
{
    HttpConfiguration config = new HttpConfiguration();

    // Swagger
    SwaggerConfig.Register(config);

    // Authentication token
    ConfigureOAuth(app);

    // SignalR configuration
    ConfigureSignalR(app);

    // Register routes
    WebApiConfig.Register(config);

    // Allow cross-domain requests
    app.UseCors(CorsOptions.AllowAll);

    app.UseWebApi(config);
}

SwaggerConfig.cs

using Swashbuckle.Application;
using System.Web.Http;

namespace Name.API 
{
     public class SwaggerConfig
     {
          public static void Register(HttpConfiguration config)
          {
                config.EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "Name.API");   
                })
            .EnableSwaggerUi(c =>
                {
                });
         }
     }
}

我在https://github.com/domaindrivendev/Swashbuckle/issues/196找到了解决方案

【讨论】:

    【解决方案2】:

    我在 Owin + Swashbuckle 集成方面遇到了很多问题。上面给出的解决方案是不完整的,我必须弄清楚许多其他事情以确保兼容性。我的最终结果是在这个 repo 中开源的,可以用作任何需要它的人的模板!

    请查看:ASPSwaggerOwinTemplate

    【讨论】:

    • 请详细说明您遇到了哪些问题。
    猜你喜欢
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 2021-12-20
    • 2019-11-29
    • 2022-11-03
    相关资源
    最近更新 更多