【问题标题】:Swagger UI Not LoadingSwagger UI 未加载
【发布时间】:2019-01-09 10:23:38
【问题描述】:

Swagger UI 未加载,Json 按预期加载,但支持的 js、css 文件存在问题。

【问题讨论】:

  • 您能否在将 Swagger UI 添加到您的应用程序的位置添加代码?
  • 刚刚想通了,我在配置下写了“c.RoutePrefix = string.Empty;”...所以我需要像这样访问:localhost:<port>——然后 Swagger 正在加载,/不需要招摇。感谢您的帮助:)

标签: .net asp.net-core swagger swagger-ui swagger-2.0


【解决方案1】:

尝试复制项目中vendor/swagger-api/swagger-ui 中的/dist 目录。我不确定正确的方法,但我遇到了同样的问题,它对我有用。另外,请尝试提供有关您所面临问题的更多详细信息,也可能是代码 sn-ps。 或者,尝试以下方法:

  1. 检查所有控制器方法是否都有 [http] 标签。如果它们都可以但仍然无法正常工作,请转到第 2 步
  2. 在你的配置函数中确保你有 app.UseStaticFiles();如果它仍然不起作用,请转到第 3 步
  3. 卸载并重新安装 swagger。如果它不起作用,请转到第 4 步(仅限核心)
  4. 如果您正在使用 Core 安装 Microsoft.AspNetCore.StaticFiles 并在您的项目中引用它。

【讨论】:

    【解决方案2】:

    出现此问题的原因您需要按照以下步骤操作:

    1. 在 Startup.cs 文件下有一个方法“ConfigureServices”,执行以下操作:

      public void ConfigureServices(IServiceCollection services)
      {
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
      
          // Configure database connection
          services.Configure<Settings>(options =>
          {
              options.ConnectionString = Configuration.GetSection("database:ConnectionString").Value;
              options.Database = Configuration.GetSection("Db:Database").Value;
          });
      
          //register the RecordedMediaContext dependency here
          services.AddTransient<ITestService, TestService>();
          // Register the Swagger generator, defining 1 or more Swagger documents
          services.AddSwaggerGen(c =>
          {
              c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
      
              // Set the comments path for the Swagger JSON and UI.
              var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
              var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
              c.IncludeXmlComments(xmlPath);
          });
      
          // Max file upload sixe 5gb =5,368,709,120 bytes
          services.Configure<FormOptions>(x => x.MultipartBodyLengthLimit = 5368709120);
      }
      
    2. 然后在同一个 Startup.cs 文件下的 Configure 方法下添加以下代码

      public void Configure(IApplicationBuilder app, IHostingEnvironment env)
      {
          string baseApiUrl = Configuration.GetSection("BaseApiUrl").Value;
          // Enable middleware to serve generated Swagger as a JSON endpoint.
          app.UseSwagger();
      
          // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
          // specifying the Swagger JSON endpoint.
          app.UseSwaggerUI(c =>
          {
              #if DEBUG
                  // For Debug in Kestrel
                  c.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
              #else
                  // To deploy on IIS
                  c.SwaggerEndpoint(""+baseApiUrl+"/swagger/v1/swagger.json", "My API V1");
              #endif
          });
      
          if (env.IsDevelopment())
          {
              app.UseDeveloperExceptionPage();
          }
          else
          {
              // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
              app.UseHsts();
          }
      
          //Accept All HTTP Request Methods from all origins
          //app.UseCors(builder => builder.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());
      
          app.UseHttpsRedirection();
          #if !DEBUG
              app.UseDefaultFiles();
              app.UseStaticFiles();
          #endif
          app.UseMvc();
      }
      

      谢谢。

    【讨论】:

    • BaseApiUrl 是什么?
    • BaseApiUrl 将是域名,如果您正在调试应用程序,那么它使用 localhost:portnumber//swagger/index.html 并且如果托管在某个地方,那么它的域名。比如 baseApiUrl= localhost:port number
    • 为什么需要添加它? Swagger 应该绑定到它正在监听的内容,并且您的反向代理应该只是将请求传递给 API 并返回响应。
    • 是的,你是对的,当我将它部署在 IIS 上时它不起作用。我尝试了两种方式,它开始以这种方式工作。
    • 没有运气仍然需要Css,JS文件没有加载
    【解决方案3】:

    我遇到了同样的问题:Swagger 在本地工作得很好,但一旦发布到 IIS 就不会显示 UI。通过将允许的.js.json 文件添加到IIS 的请求过滤功能,该问题已得到解决。对于新创建的网站,这些文件默认不会在此处列出。

    【讨论】:

      【解决方案4】:

      您是否在启动时启用了静态文件?

      app.UseStaticFiles();
      

      【讨论】:

      • 已经添加,但没有运气。无法加载所需的 JS、Swagger UI 的 Css。
      【解决方案5】:

      尝试删除 .vs 文件夹,Visual Studio 在构建项目时会自动重新创建它。删除后 Swagger Page 工作正常。

      根据 Tyler Findlay 的回答。 https://stackoverflow.com/a/50629590/9705947

      【讨论】:

        【解决方案6】:

        我在 msdoc 上关注原始的 guide 试图启用 swagger 支持,在使用 RoutePrefix 添加此块后,我没有意识到路径从 http://localhost:&lt;port&gt;/swagger 更改为 http://localhost:&lt;port&gt;,感谢 Sampat 指出这一点。

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            c.RoutePrefix = string.Empty; // this changes the default path
        });
        

        【讨论】:

          猜你喜欢
          • 2021-09-01
          • 2016-09-06
          • 2021-07-14
          • 1970-01-01
          • 2018-02-12
          • 2015-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多