虚拟目录 Swagger UI 端点中的 IIS 站点
IIS site within virtual directory Swagger UI end point
Swagger UI 端点与暂存中的 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 的关系比与环境变量的关系更大。 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");
});
我花了一些时间才得到它运行所以我想在这里分享我的解决方案
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);
});
}
我在 Swagger 中更改了这一行 UI 配置 (Startup.cs):
c.SwaggerEndpoint("/prueba/swagger/v1/swagger.json", "Swagger (....)");
添加“../”适用于托管在虚拟目录下和没有虚拟目录的网站
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", "TestService");
});
不幸的是,None 适合我。
我都试过了。
工作解决方案:
app.UseSwagger(c => {
c.RouteTemplate = "swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("v1/swagger.json", "My API V1");
});
对我有用的是,
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("swagger/v1/swagger.json", "MyDevOpsAPI V1");
});
请注意,我已经删除了前导“/”。
Swagger UI 端点与暂存中的 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 的关系比与环境变量的关系更大。 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");
});
我花了一些时间才得到它运行所以我想在这里分享我的解决方案
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);
});
}
我在 Swagger 中更改了这一行 UI 配置 (Startup.cs):
c.SwaggerEndpoint("/prueba/swagger/v1/swagger.json", "Swagger (....)");
添加“../”适用于托管在虚拟目录下和没有虚拟目录的网站
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("../swagger/v1/swagger.json", "TestService");
});
不幸的是,None 适合我。
我都试过了。
工作解决方案:
app.UseSwagger(c => {
c.RouteTemplate = "swagger/{documentName}/swagger.json";
});
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("v1/swagger.json", "My API V1");
});
对我有用的是,
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("swagger/v1/swagger.json", "MyDevOpsAPI V1");
});
请注意,我已经删除了前导“/”。