相关疑难解决方法(0)

ASP.NET Core - Swashbuckle没有创建swagger.json文件

我无法获得Swashbuckle.AspNetCore(1.0.0)包来生成任何输出.我读过swagger.json文件应该写成'〜/ swagger/docs/v1'.但是,我没有得到任何输出.

我从一个全新的ASP.NET Core API项目开始.我应该提到这是ASP.NET Core 2. API工作,我能够从值控制器中检索值就好了.

我的启动类具有完全如本文所述的配置(GitHub上的Swashbuckle.AspNetCore).

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

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

        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
        });
    }

    // This method gets called by the runtime. Use this method …
Run Code Online (Sandbox Code Playgroud)

c# swagger swashbuckle asp.net-core

38
推荐指数
13
解决办法
4万
查看次数

如何修复.net核心应用程序中找不到的swagger.json

我正在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 = "";
     });
Run Code Online (Sandbox Code Playgroud)

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

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 = "";
     });
 }
Run Code Online (Sandbox Code Playgroud)

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

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

c# asp.net-core asp.net-core-2.2

1
推荐指数
3
解决办法
4215
查看次数