C# Web API 2 swagger文档根路径
我有Web API 2,并使用[Swashback][1]配置了Swagger,如下所示:C# Web API 2 swagger文档根路径,c#,asp.net-web-api2,swagger-ui,C#,Asp.net Web Api2,Swagger Ui,我有Web API 2,并使用[Swashback][1]配置了Swagger,如下所示: private static void ConfigureSwagger(HttpConfiguration config) { // add the versioned IApiExplorer and capture the strongly-typed implementation (e.g. VersionedApiExplorer vs IApiExplorer) // no
private static void ConfigureSwagger(HttpConfiguration config)
{
// add the versioned IApiExplorer and capture the strongly-typed implementation (e.g. VersionedApiExplorer vs IApiExplorer)
// note: the specified format code will format the version as "'v'major[.minor][-status]"
var apiExplorer = config.AddVersionedApiExplorer(o => o.GroupNameFormat = "'v'VVV");
config
.EnableSwagger(swagger =>
{
// build a swagger document and endpoint for each discovered API version
swagger.MultipleApiVersions(
(apiDescription, version) => apiDescription.GetGroupName() == version,
info =>
{
foreach (var group in apiExplorer.ApiDescriptions)
{
var description = "AAAA API.";
if (group.IsDeprecated)
{
description += " This API version has been deprecated.";
}
info.Version(group.Name, $"AAAA API {group.ApiVersion}")
.Contact(c => c.Name("AAAA").Email("AAAA@AAAA.com"))
.Description(description)
.License(l => l.Name("AAAA").Url("www.AAAA.com"))
.TermsOfService("AAAA. All rights reserved.");
}
});
// add a custom operation filter which sets default values
swagger.OperationFilter<SwaggerDefaultValues>();
// integrate xml comments
swagger.IncludeXmlComments(XmlCommentsFilePath);
//swagger.RootUrl(req => SwaggerDocsConfig.DefaultRootUrlResolver(req) + "/api");
})
.EnableSwaggerUi(swagger => swagger.EnableDiscoveryUrlSelector());
}
private static void configurationswagger(HttpConfiguration配置)
{
//添加版本化的IApiExplorer并捕获强类型实现(例如VersionedAppiexplorer与IApiExplorer)
//注意:指定的格式代码将版本格式设置为“'v'major[.minor][状态]”
var apiExplorer=config.AddVersionedApiExplorer(o=>o.GroupNameFormat=“'v'VVV”);
配置
.EnableSwagger(swagger=>
{
//为每个发现的API版本构建一个招摇过市的文档和端点
昂首阔步(
(apiscription,version)=>apiscription.GetGroupName()==version,
信息=>
{
foreach(apiExplorer.apisdescriptions中的var组)
{
var description=“AAAA API。”;
如果(组已删除)
{
description+=“此API版本已被弃用。”;
}
info.Version(group.Name,$“AAAA API{group.ApiVersion}”)
.Contact(c=>c.Name(“AAAA”).Email(“AAAA@AAAA.com"))
.说明(说明)
.License(l=>l.Name(“AAAA”).Url(“www.AAAA.com”))
.TermsOfService(“AAAA.保留所有权利”);
}
});
//添加设置默认值的自定义操作筛选器
swagger.OperationFilter();
//集成xml注释
includexmlcoments(xmlcomentsfilepath);
//RootUrl(req=>SwaggerDocsConfig.DefaultRootUrlResolver(req)+“/api”);
})
.EnableSwaggerUi(swagger=>swagger.EnableDiscoveryUrlSelector());
}
在本地运行时,一切正常,我可以看到带有api端点的招摇过市页面。现在,当我们在默认网站\AAAA下将其部署到IIS时,路径总是解析到默认网站的根目录,而不是AAAA(WebAPI)应用程序
有人知道可能是什么问题吗?回答
托管在IIS中的OWIN-不正确的VirtualPath处理
当您在OWIN/SystemWeb上托管WebAPI2时,默认情况下Swashback无法正确解析VirtualPathoot
您必须在启动时在HttpConfiguration中显式设置VirtualPatroot,或者执行如下自定义以修复自动发现:
httpConfiguration.EnableSwagger(c =>
{
c.RootUrl(req =>
req.RequestUri.GetLeftPart(UriPartial.Authority) +
req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));
}
答复
托管在IIS中的OWIN-不正确的VirtualPath处理
当您在OWIN/SystemWeb上托管WebAPI2时,默认情况下Swashback无法正确解析VirtualPathoot
您必须在启动时在HttpConfiguration中显式设置VirtualPatroot,或者执行如下自定义以修复自动发现:
httpConfiguration.EnableSwagger(c =>
{
c.RootUrl(req =>
req.RequestUri.GetLeftPart(UriPartial.Authority) +
req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));
}
谢谢。好久没找到了,非常感谢。我花了很长时间才找到它