如何配置 Swashbuckle 以显示 API 版本而不是 v{version} 变量?



我正在使用Swashbuckle来记录我的Web API 2.2 API。当我加载 Swagger 页面时,uri 显示的是版本占位符变量,而不是实际版本。 例如:

/api/v{version}/authentication

而不是:

/api/v2/authentication

如何将我的应用程序或 Swashbuckle 配置为显示版本号而不是版本变量?

更新了 WebApiConfig 的代码:

// Web API configuration and services
var constraintResolver = new System.Web.Http.Routing.DefaultInlineConstraintResolver()
{
ConstraintMap =
{
["apiVersion"] = typeof(Microsoft.Web.Http.Routing.ApiVersionRouteConstraint)
}
};
config.AddVersionedApiExplorer(opt =>
{
opt.SubstituteApiVersionInUrl = true;
});
config.MapHttpAttributeRoutes(constraintResolver);
config.AddApiVersioning();
// Web API routes
//config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

一些参考资料 招摇

抱歉,刚刚注意到您正在谈论 URI...不确定以下是否有帮助

您是否在招摇的配置中尝试过以下内容:

public static void Register(HttpConfiguration config)
{
config
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "version api");                
c.PrettyPrint();
c.OAuth2("oauth2").Description("OAuth2 ResourceOwner Grant").TokenUrl("/testtoken");
c.IncludeXmlComments(GetXmlCommentsPath());
c.DocumentFilter<AuthTokenOperation>();
c.DocumentFilter<ListManagementSwagger>();
c.SchemaFilter<SchemaExamples>();
})
.EnableSwaggerUi(c =>
{
c.DocumentTitle("test webapi");                
});
}

这是实现版本控制的方法之一。我有一个自定义标头和自定义根网址功能,您可以忽略该部分。 此代码要求 Swagger 从提供的 xml 构建两个不同的版本。

public class SwaggerConfig
{
public static void Register()
{
var customHeader = new SwaggerHeader  //you can ignore this one
{
Description = "Custom header description",
Key = "customHeaderId",
Name = "customHeaderId"
};
var versionSupportResolver = new Func<ApiDescription, string, bool>((apiDescription, version) =>
{
var path = apiDescription.RelativePath.Split('/');
var pathVersion = path[1];
return string.Equals(pathVersion, version, StringComparison.OrdinalIgnoreCase);
});
var versionInfoBuilder = new Action<VersionInfoBuilder>(info => {
info.Version("v2", "My API v2");
info.Version("v1", "My API v1");
});
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
//c.RootUrl(ComputeHostAsSeenByOriginalClient);  //you can ignore this custom function
c.Schemes(new[] { "http", "https" });
customHeader.Apply(c);
c.MultipleApiVersions(versionSupportResolver, versionInfoBuilder);
c.IgnoreObsoleteActions();
c.IncludeXmlComments(GetXmlCommentsPath());
c.DescribeAllEnumsAsStrings();
})
.EnableSwaggerUi("swagger/ui/{*assetPath}", c =>
{
c.DisableValidator();
c.SupportedSubmitMethods("GET", "POST");
});
}
private static Func<XPathDocument> GetXmlCommentsPath()
{
return () =>
{
var xapixml = GetXDocument("My.API.xml");
var xElement = xapixml.Element("doc");
XPathDocument xPath = null;
if (xElement != null)
{
using (var ms = new MemoryStream())
{
var xws = new XmlWriterSettings { OmitXmlDeclaration = true, Indent = false };
using (var xw = XmlWriter.Create(ms, xws))
{
xElement.WriteTo(xw);
}
ms.Position = 0;
xPath = new XPathDocument(ms);
}
}
return xPath;
};
}
private static XDocument GetXDocument(string file)
{
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin");
var xDoc = XDocument.Load(path + "\" + file);
return xDoc;
}
//ComputeHostAsSeenByOriginalClient function code
}

最新更新