虚拟目录中的IIS站点Swagger UI端点

时间:2017-07-21 02:22:29

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

Swagger UI端点与登台时的dev(不包括域名)不同

IIS配置

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

使用上述配置

开发

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

}

分段

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

}

启动计算机上的用户界面的终点

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

6 个答案:

答案 0 :(得分:7)

问题与swagger比环境变量更相关。 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");
            });

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

不幸的是,他们都不适合我。
我已经尝试了所有这些。

工作解决方案:

app.UseSwagger(c => {
    c.RouteTemplate = "swagger/{documentName}/swagger.json";
});

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

答案 4 :(得分:0)

对我有用的是,

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

请注意,我已经删除了前导“/”。

答案 5 :(得分:-1)

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

insertMany