【问题标题】:C# web api swagger integrationC# web api 招摇集成
【发布时间】:2017-01-17 09:36:12
【问题描述】:

我在将 swagger ui 与我的 web api 集成时遇到问题,我不知道是什么问题。

当我在浏览器中大摇大摆地调用时,页面http://localhost:56381/swagger/ui/index 就像这个截图中一样

在 SwaggerConfig.cs 文件中,我有以下代码:

[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

 namespace dummyNamespace
 {
    public class SwaggerConfig
    {
       public static void Register()
      {
        var thisAssembly = typeof(SwaggerConfig).Assembly;
        GlobalConfiguration.Configuration
            .EnableSwagger(c => c.SingleApiVersion("v1", "Test API"))
            .EnableSwaggerUi();

       }
    }
  }

我遵循本教程:http://www.wmpratt.com/swagger-and-asp-net-web-api-part-1/。但不工作。

我现在不知道我的配置有什么问题。

我使用.net framework 4.5.2、Web api 2.0和Swashbuckle.5.5.3版本

更新: 当我调用这个网址时

http://localhost:56381/swagger/docs/v1

返回此图像:

更新1:

在我将此代码放入我的 WebApiConfig.cs 之后:

var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);

现在http://localhost:56381/swagger/ui/index 返回这个 json:

  {
    "statusCode": 200
  }

任何想法如何使http://localhost:56381/swagger/ui/index返回此页面:

该页面来自一个测试项目。

【问题讨论】:

  • 你为什么这么相信?
  • 尝试点击链接,它是一个png文件。链接是png的描述
  • @MegaTron 我的 Startup.cs 位于我项目的根目录中。并包含:code public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); } }
  • @MegaTron 对不起。坏链接。这是正确的:wmpratt.com/swagger-and-asp-net-web-api-part-1。 chrome 中的多个选项卡和错误的复制粘贴。
  • 你能发布你的控制器吗?响应不是错误,因此可能没有其他可描述的......

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


【解决方案1】:

阅读您发布的教程后,您的根 URL 似乎与 swagger 所期望的有所不同。从招摇配置文件:

// 默认情况下,服务根 url 是从用于访问文档的请求中推断出来的。 // 但是,在某些情况下(例如代理和负载平衡环境)可能不这样做 // 正确解析。您可以通过提供自己的代码来确定根 URL 来解决此问题。 // //c.RootUrl(req => GetRootUrlFromAppConfig());

如果不是这种情况,则可能是 swashbuckle nuget 安装以某种方式损坏。尝试删除 nuget 包并重新安装它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2020-02-02
    • 2014-10-15
    相关资源
    最近更新 更多