【问题标题】:How to fix swagger.json not found in .net core 2.2 app如何修复 .net core 2.2 应用程序中未找到的 swagger.json
【发布时间】:2019-06-21 18:10:28
【问题描述】:

我正在 IIS 服务器上部署我的 .net 核心应用程序,并在 swagger UI 中遇到未找到 swagger.json 的问题。当我在本地(开发环境)运行它时,一切正常,但是当我将它部署在 IIS 服务器上时,它找不到 swagger.json 文件。

以前我在 .net core 2.1 应用程序中遇到过这个问题,我通过编写下面的代码来获取虚拟基本路径来解决它。

string basePath = Environment.GetEnvironmentVariable("ASPNETCORE_APPL_PATH");
            basePath = basePath == null ? "/" : (basePath.EndsWith("/") ? basePath : $"{basePath}/");

 app.UseSwaggerUI(c =>
     {
      c.SwaggerEndpoint($"{basePath}swagger/v1/swagger.json", "Test.Web.Api");
      c.RoutePrefix = "";
     });

我尝试了下面的代码来解决它:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
   app.UseSwaggerUI(c =>
     {
      c.SwaggerEndpoint($"{env.ContentRootPath}swagger/v1/swagger.json", "Test.Web.Api");
       //OR
      c.SwaggerEndpoint($"{env.WebRootPath}swagger/v1/swagger.json", "Test.Web.Api");
      c.RoutePrefix = "";
     });
 }

但是上面的代码不起作用,因为它返回的是实际的物理路径而不是虚拟路径。

有谁知道如何在 .net core 2.2 中获取虚拟路径,因为Environment.GetEnvironmentVariable("ASPNETCORE_APPL_PATH"); 不起作用。任何线索都会有所帮助。

【问题讨论】:

  • 根据 Swashbuckle 文档,您应该能够只使用“./swagger/v1/swagger.json”而不是预先添加托管 URL。那没有用吗?编辑为在路径前面添加句点,因为我注意到它没有在域的根目录下运行。
  • 是的,我也尝试过,但没有成功。获取 swagger.json 文件失败。
  • 我有类似的问题,但原因可能不同。如果有人想可以查看我的经验here

标签: c# asp.net-core asp.net-core-2.2


【解决方案1】:

我已通过将以下代码放入我的 .net 核心应用程序中解决了我的问题。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {
   app.UseSwaggerUI(c =>
     {
      c.SwaggerEndpoint("./swagger/v1/swagger.json", "Test.Web.Api");
      c.RoutePrefix = string.Empty;
    });
 }

根据swashbuckle 文档,如果您将其托管在 IIS 中,则需要在前面添加 ./。

我希望这个答案能节省您的时间!

【讨论】:

  • 是的,但我错过了“。”前 ”/”。谢谢!
  • 不幸的是,这不起作用。我有使用 IIS express 托管的应用程序,并将launchsettings.json 设置为"iisExpress": { "applicationUrl": "http://localhost/char", 以测试 IIS 虚拟目录。但是问题是 localhost/char/swagger 显示 404 错误但 localhost/char/swagger/v1/swagger.json 有效(显示 json 文件)。如果applicationUrl 值中没有路径,localhost/swagger 工作正常。我正在使用netcore31Swashbuckle.AspNetCore 5.0.0
  • @VAAA 是的,我做到了。请在此线程中查看my answer
  • 这个解决方案救了我的命!
【解决方案2】:

我尝试使用Mahesh's answer 中的c.SwaggerEndpoint("./swagger/v1/swagger.json", "Test.Web.Api");,但问题是它确实有帮助。

我已经使用 IIS Express 从 VS 开始 Swashbuckle sample

我遇到了错误

未能加载 API 定义。未定义的./swagger/v1/swagger.json

何时从 Web 浏览器访问 localhost/char/swagger 端点。

我修改了launchsettings.json\iisExpress\applicationUrl 并添加了类似"iisExpress": { "applicationUrl": "http://localhost/char" 的路径 和Startup.cs 之类的源代码

   app.UseSwaggerUI(c =>
     {
      c.SwaggerEndpoint("./swagger/v1/swagger.json", "Test.Web.Api");
      c.RoutePrefix = string.Empty;
    });

解决方案在Github issue找到,请在下面找到:

app.UseSwaggerUI(c =>
{
    string swaggerJsonBasePath = string.IsNullOrWhiteSpace(c.RoutePrefix) ? "." : "..";
    c.SwaggerEndpoint($"{swaggerJsonBasePath}/swagger/v1/swagger.json", "My API");
});

我没有在文档中看到它,但它正在工作

  • 由 IIS Express 在本地托管,在 iisExpress\applicationUrl 中的主机名之后有(或没有)附加路径
  • 由 IIS 在暂存环境中托管(在主机名之后有附加路径,例如 http://staginghost/char/swagger

【讨论】:

  • @GabrielSimas 这个解决方案已有两年历史了。尝试发布新的 Swashbuckle.WebApi 问题,您认为它做得正确。
【解决方案3】:

对于 .net core 5,swagger 会在项目创建时自动添加。

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyProject.Api", Version = "v1" });
    });
}

但是,您需要确保两个注释行不在 if 块中。

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IServiceProvider svp)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                //app.UseSwagger();
                //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProject.Api v1"));
            }

            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProject.Api v1"));                 

        }

【讨论】:

    【解决方案4】:

    我遇到了这个问题,发现你必须匹配路径中版本的确切类型以及 servicescollection 和 appbuilder

     services.AddSwaggerGen(c => {
        c.SwaggerDoc("v1" , new OpenApiInfo{ Title = "MyApp API" , Version = "v1" });
     });
    
     app.UseSwagger(); app.UseSwaggerUI(c => { 
        c.SwaggerEndpoint("/swagger/v1/swagger.json","MyApp API v1"); });
    

    【讨论】:

      【解决方案5】:

      在我的情况下,我忘记在我的控制器公共 get 方法之一中添加 HTTPGet 属性

      【讨论】:

        【解决方案6】:

        您可以通过以下方式修复相同的问题:

        包:#region Assembly Swashbuckle.AspNetCore.Swagger,版本=4.0.1.0

        框架:.Net Core 2.2

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            // 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 =>
            {
                 c.SwaggerEndpoint("/swagger/v1/swagger.json", "Versioned Api v1");
            );
        }
        

        当您在本地运行应用程序或托管在 IIS 上时,这是可行的。

        【讨论】:

          【解决方案7】:

          对于 .Net Core 2.2

            // This method gets called by the runtime. Use this method to add services to the container.
              public void ConfigureServices(IServiceCollection services)
              {
                  services.AddSwaggerDocument();
          

          然后

           // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
              public void Configure(IApplicationBuilder app, IHostingEnvironment env)
              {
                  if (env.IsDevelopment())
                  {
                      app.UseDeveloperExceptionPage();
                      app.UseOpenApi();
                      app.UseSwaggerUi3();
          
                  }
                  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();
                  }
          

          【讨论】:

          • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-28
          • 2019-01-22
          • 1970-01-01
          • 2019-08-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多