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

asp.net core-虚拟目录Swagger UI端点内的IIS站点

(asp.net core - IIS site within virtual directory Swagger UI end point)

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

Swagger UI端点在暂存阶段与开发人员不同(不包括域名)

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/"

}

暂存为ON的开发机上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
Questioner
Jay
Viewed
0
Jay 2017-07-26 15:20:31

问题比环境变量更重要。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");
            });