为什么Swashbuckle会在我的所有路由中附加一个版本后缀?

时间:2017-08-02 09:30:07

标签: iis swashbuckle

我遵循了WS托管API的Swashbuckle安装手册。我安装了NuGet包并将其部署在IIS上。该文档现已可用。但是所有网址都以“-v1”为后缀,例如“/ api / v1 / myresource-v1()”。

因此,IIS抱怨此URL没有路由。 “/ api / v1 / myresource”是正确的。

为什么会发生这种情况,如何删除所有路由的此后缀?

谢谢。

1 个答案:

答案 0 :(得分:0)

我没有针对此问题的直接解决方案,但此解决方法对我有用:

c.MultipleApiVersions((apiDesc, targetApiVersion) =>
    {
        // remove any Version text from the tags                   
        apiDesc.ActionDescriptor.ControllerDescriptor.ControllerName = Regex.Replace(apiDesc.ActionDescriptor.ControllerDescriptor.ControllerName, @"v([\d]+)", string.Empty, RegexOptions.IgnoreCase);
        // remove -vX() from Controller Names relativ path
        apiDesc.RelativePath = Regex.Replace(apiDesc.RelativePath, @"(-v[\d]+\(\))", string.Empty, RegexOptions.IgnoreCase);
        // replace the relative Path Apiverision with the current api version
        apiDesc.RelativePath = Regex.Replace(apiDesc.RelativePath, @"(v[{]+version[}])", targetApiVersion, RegexOptions.IgnoreCase);
        var apiParameterDescription = apiDesc.ParameterDescriptions.FirstOrDefault(p => p.Name == "version");
        if (apiParameterDescription != null)
        { 
            apiDesc.ParameterDescriptions.Remove(apiParameterDescription);
        }

        // now filter out any controllers that aren't the target version
        var controllerNamespace = apiDesc.ActionDescriptor.ControllerDescriptor.ControllerType.FullName;

        return CultureInfo.InvariantCulture.CompareInfo.IndexOf(controllerNamespace, $".{targetApiVersion}.", CompareOptions.IgnoreCase) >= 0 &&
        apiDesc.RelativePath.Contains($"{targetApiVersion}/");
    },
    vc =>
        {
            vc.Version("v4", "Api V4");
            vc.Version("v3", "Api V3");
            vc.Version("v2", "Api V2");
            vc.Version("v1", "Api V1");
        }
);