IIS site within virtual directory Swagger UI end point

梦想的初衷 提交于 2020-01-10 04:08:06

问题


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

IIS Configuration

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

回答1:


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



回答2:


It took me some time to get it running so i want to share my solution here

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


来源:https://stackoverflow.com/questions/45227986/iis-site-within-virtual-directory-swagger-ui-end-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!