Warm tip: This article is reproduced from serverfault.com, please click

IIS site within virtual directory Swagger UI end point

发布于 2017-07-21 02:22:29

Swagger UI end point is not same as dev in staging ( excluding domain names)

IIS Configuration

enter image description here

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();

with the above configuration

Development

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

}

Staging

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

}

The end point for UI on the dev machine with staging ON

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

but the same one on the staging server

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

instead of ( what it should be)

http://xxxx:5002/Api/api-docs/v1/swagger.json
Questioner
Jay
Viewed
0
Jay 2017-07-26 15:20:31

The problem is more relevant to swagger than Environment variable. Swagger does support the virtual directory which then the configuration should look like below. Note that virtual directory doesn't affect the UI End point.

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");
            });