简体   繁体   English

Web API 2 swagger文档的根路径

[英]Web API 2 swagger documentation root path

I have Web API 2 and configured Swagger using [Swashbuckle][1] as such: 我有Web API 2,并使用[Swashbuckle] [1]配置了Swagger,如下所示:

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

}

When running locally everything is working fine I can see swagger page with api endpoints. 在本地运行时,一切运行正常,我可以看到带有api端点的大页面。 Now when we deploy this into our IIS under Default Web Site\\AAAA the path is always being resolved to the root of the Default Web Site and not AAAA (WebAPI) application 现在,当我们将其部署到默认网站\\ AAAA下的IIS中时,路径始终被解析为默认网站的根目录,而不是AAAA(WebAPI)应用程序的根目录 在此处输入图片说明

Does anyone know what could be the issue? 有谁知道可能是什么问题?

Answer from here. 这里回答

OWIN Hosted in IIS - Incorrect VirtualPathRoot Handling When you host Web API 2 on top of OWIN/SystemWeb, Swashbuckle cannot correctly resolve VirtualPathRoot by default. OWIN托管在IIS中-错误的VirtualPathRoot处理当您在OWIN / SystemWeb之上托管Web API 2时,默认情况下Swashbuckle无法正确解析VirtualPathRoot。

You must either explicitly set VirtualPathRoot in your HttpConfiguration at startup, or perform customization like this to fix automatic discovery: 您必须在启动时在HttpConfiguration中显式设置VirtualPathRoot,或执行如下自定义操作以修复自动发现:

httpConfiguration.EnableSwagger(c => 
{
    c.RootUrl(req =>
        req.RequestUri.GetLeftPart(UriPartial.Authority) +
        req.GetRequestContext().VirtualPathRoot.TrimEnd('/'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM