首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Web API 2 swagger文档根路径

Web API 2 swagger文档根路径
EN

Stack Overflow用户
提问于 2018-01-11 01:46:02
回答 1查看 2.6K关注 0票数 0

我有Web API 2,并使用Swashbuckle配置了Swagger:

代码语言:javascript
复制
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());

}

在本地运行时,一切运行正常,我可以看到带有api端点的swagger页面。现在,当我们将其部署到默认网站\ AAAA下的IIS时,路径总是解析到默认网站的根目录,而不是AAAA (WebAPI)应用程序

有人知道问题出在哪里吗?

EN

回答 1

Stack Overflow用户

发布于 2018-01-13 04:59:31

来自here.的答案

托管在IIS中的OWIN -不正确的VirtualPathRoot处理当您在OWIN/SystemWeb上托管WebAPI2时,默认情况下Swashbuckle无法正确解析VirtualPathRoot。

您必须在启动时在HttpConfiguration中显式设置VirtualPathRoot,或者像这样执行自定义以修复自动发现:

代码语言:javascript
复制
httpConfiguration.EnableSwagger(c => 
{
    c.RootUrl(req =>
        req.RequestUri.GetLeftPart(UriPartial.Authority) +
        req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48193263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档