【问题标题】:IIS site within virtual directory Swagger UI end point虚拟目录中的 IIS 站点 Swagger UI 端点
【发布时间】:2021-07-26 23:54:17
【问题描述】:

Swagger UI 端点与 staging 中的 dev 不同(不包括域名)

IIS 配置

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

 app.UseSwagger(c=>
        {
            //Change the path of the end point , should also update UI middle ware for this change                
            c.RouteTemplate = "api-docs/{documentName}/swagger.json";                 
        });          

        app.UseSwaggerUI(c =>
        {  
            //Include virtual directory if site is configured so
            c.SwaggerEndpoint(Configuration["Appsettings:VirtualDirectory"]+"api-docs/v1/swagger.json", "Api v1");                
        });

 services.AddSwaggerGen(c =>
        {
 var xmlDocPath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Api.xml");
            c.IncludeXmlComments(xmlDocPath);
            c.DescribeAllEnumsAsStrings();

以上配置

发展

 "AppSettings": {
"VirtualDirectory": "/"

}

分期

 "AppSettings": {
"VirtualDirectory": "/Api/"

}

开发机器上的 UI 的终点,登台开启

http://localhost:5001/api-docs/v1/swagger.json

但在登台服务器上是同一个

http://xxxx:5002/swagger/Api/api-docs/v1/swagger.json

而不是(应该是什么)

http://xxxx:5002/Api/api-docs/v1/swagger.json

【问题讨论】:

  • 展示你如何在开发机器上将环境设置为暂存,因为现在看起来你做错了。您是否将 ASPNETCORE_ENVIRONMENT 变量设置为 Staging 或您使用的变量?
  • 没错,ASPNETCORE_ENVIRONMENT 变量。

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


【解决方案1】:

这个问题与环境变量更相关。 Swagger 确实支持虚拟目录,那么配置应该如下所示。请注意,虚拟目录不会影响 UI 端点。

app.UseSwagger(c =>
            {
                //Change the path of the end point , should also update UI middle ware for this change                
                c.RouteTemplate = "api-docs/{documentName}/swagger.json";
            });
 app.UseSwaggerUI(c =>
            {
                //Include virtual directory if site is configured so
                c.RoutePrefix = "api-docs";
                c.SwaggerEndpoint("v1/swagger.json", "Api v1");
            });

【讨论】:

    【解决方案2】:

    添加“../”适用于托管在虚拟目录下且没有虚拟目录的网站

    app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("../swagger/v1/swagger.json", "TestService");
            });
    

    【讨论】:

    • 这对我有用。谢谢
    【解决方案3】:

    我在 Swagger UI 配置 (Startup.cs) 中更改了这一行:

    c.SwaggerEndpoint("/prueba/swagger/v1/swagger.json", "Swagger (....)");
    

    【讨论】:

      【解决方案4】:

      不幸的是,它们都不适合我。
      我都试过了。

      工作解决方案:

      app.UseSwagger(c => {
          c.RouteTemplate = "swagger/{documentName}/swagger.json";
      });
      
      app.UseSwaggerUI(c => {
          c.SwaggerEndpoint("v1/swagger.json", "My API V1");
      });
      

      【讨论】:

        【解决方案5】:

        对我有用的是,

                    app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("swagger/v1/swagger.json", "MyDevOpsAPI V1");
                });
        

        请注意,我已经删除了开头的“/”。

        【讨论】:

          【解决方案6】:

          我花了一些时间让它运行,所以我想在这里分享我的解决方案

              string vpath = s.GetValue<string>("VirtualPath") ?? string.Empty;
          
              if (string.IsNullOrWhiteSpace(vpath))
              {
                  app.UseSwagger();
                  app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", endpointName); });
              }
              else
              {
                      app.UseSwagger(c =>
                      {
                          //no virtual path in the roue template it is relative 
                          c.RouteTemplate = $"swagger/{{documentName}}/swagger.json";
                          //c.PreSerializeFilters.Add((swagger, request) => swagger.BasePath = $"/{vpath}");
                      });
                      app.UseSwaggerUI(options =>
                      {
                          //options.RoutePrefix = vpath;
                          //gives the location of the gennerated json file to the UI
                          //start with / to create an absolute path from the base directory
                          options.SwaggerEndpoint($"/{vpath}/swagger/v1/swagger.json", endpointName);
                      });
              }
          

          【讨论】:

          • s.GetValue,s 是谁?
          • @LeresAldtai 配置部分。后来我最终使用了 NSwag,它对虚拟目录没有更多问题
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-24
          • 1970-01-01
          • 1970-01-01
          • 2018-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多