Asp.net-Core

虛擬目錄中的 IIS 站點 Swagger UI 端點

  • July 26, 2021

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

與環境變數相比,該問題與招搖更相關。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");
           });

引用自:https://stackoverflow.com/questions/45227986