如何在 Swagger 中将 1.1 API 版本设置为默认版本
How to set 1.1 API version as default in Swagger
我有这样的设置,将我的 v1 设置为默认值:
services.AddApiVersioning(config =>
{
config.DefaultApiVersion = new ApiVersion(1, 0);
config.AssumeDefaultVersionWhenUnspecified = true;
});
如何将 1.1 设置为 DefaultApiVersion
? ApiVersion
没有字符串、双精度或浮点构造函数
尝试添加:
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
我猜你误解了这个设置。
你是这样设置的吗:
services.AddApiVersioning(config =>
{
config.DefaultApiVersion = new ApiVersion(1.1, 0);
config.AssumeDefaultVersionWhenUnspecified = true;
});
会报错
查看关于ApiVersion
的源代码,描述如下:
// Summary:
// Initializes a new instance of the Microsoft.AspNetCore.Mvc.ApiVersion class.
//
// Parameters:
// majorVersion:
// The major version.
//
// minorVersion:
// The minor version.
public ApiVersion(int majorVersion, int minorVersion)
: this(null, majorVersion, minorVersion, null)
{
}
所以,你只需要设置:
//version 1.1
config.DefaultApiVersion = new ApiVersion(1, 1);
我有这样的设置,将我的 v1 设置为默认值:
services.AddApiVersioning(config =>
{
config.DefaultApiVersion = new ApiVersion(1, 0);
config.AssumeDefaultVersionWhenUnspecified = true;
});
如何将 1.1 设置为 DefaultApiVersion
? ApiVersion
尝试添加:
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
我猜你误解了这个设置。
你是这样设置的吗:
services.AddApiVersioning(config =>
{
config.DefaultApiVersion = new ApiVersion(1.1, 0);
config.AssumeDefaultVersionWhenUnspecified = true;
});
会报错
查看关于ApiVersion
的源代码,描述如下:
// Summary:
// Initializes a new instance of the Microsoft.AspNetCore.Mvc.ApiVersion class.
//
// Parameters:
// majorVersion:
// The major version.
//
// minorVersion:
// The minor version.
public ApiVersion(int majorVersion, int minorVersion)
: this(null, majorVersion, minorVersion, null)
{
}
所以,你只需要设置:
//version 1.1
config.DefaultApiVersion = new ApiVersion(1, 1);