Lee*_*e Z 7 c# asp.net-routing asp.net-core-webapi .net-8.0
我最近将 webapi 升级到 .NET 8。当我测试控制器时,ApiVersion 属性在 swagger 页面中不起作用。
[ApiVersion("1.0")]
[Route("v{version:apiVersion}/api/[controller]")]
[ApiController]
[Authorize]
public class SubscriptionController(ISubscriptionDataService subscriptionDataService,
IConfigurationService configurationService) : ControllerBase
Run Code Online (Sandbox Code Playgroud)
在 swagger 页面中,默认情况下不会填写版本,就像在 .NET 7 中一样。
以前,使用默认模板weatherforecast 控制器时,行为是默认填充的。
该控制器的代码如图所示。
[ApiVersion("1.0")]
[Route("v{version:apiVersion}/api/[controller]")]
[ApiController]
[Authorize]
public class WeatherForecastController : ControllerBase
Run Code Online (Sandbox Code Playgroud)
在 Program.cs 中,添加以下内容以执行版本控制。
private static void AddApiVersioning(IServiceCollection services)
{
services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = false;
options.ReportApiVersions = false;
});
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
}
Run Code Online (Sandbox Code Playgroud)
编辑1
由于以下弃用,完成此操作的方式需要更改。
Microsoft.AspNetCore.Mvc.Versioning -> Asp.Versioning.Mvc Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer -> Asp.Versioning.Mvc.ApiExplore
在Program.cs中,需要完成以下操作:
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1, 0);
options.AssumeDefaultVersionWhenUnspecified = false;
options.ReportApiVersions = false;
}).AddApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl=true;
});
var app = builder.build();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
var descriptions = app.DescribeApiVersions();
foreach (var description in descriptions)
{
var url = $"/swagger/{description.GroupName}/swagger.json";
var name = description.GroupName.ToUpperInvariant();
options.SwaggerEndpoint(url, name);
}
});
Run Code Online (Sandbox Code Playgroud)
在控制器中, [ApiVersion("1.0")] 属性应替换为 [Asp.Versioning.ApiVersion("1.0")]。
这在生成的 Microsoft webapi 模板中有效,但如果使用其他 swagger 属性并具有其他中间件,仍然会失败(即必须输入版本)。
编辑2
经过大量实验后,即使在 Microsoft 测试项目中,类级别和方法级别的多个路由似乎也不再起作用。
不在 Swagger 中工作
using Microsoft.AspNetCore.Mvc;
namespace webapitest.Controllers;
[Asp.Versioning.ApiVersion("1.0")]
[Route("v{version:apiVersion}/api/[controller]")]
[ApiController]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet]
[Route(("/Weather"))]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
昂首阔步地工作
using Microsoft.AspNetCore.Mvc;
namespace webapitest.Controllers;
[Asp.Versioning.ApiVersion("1.0")]
[ApiController]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpGet]
[Route(("v{version:apiVersion}/api/[controller]/Weather"))]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}
Run Code Online (Sandbox Code Playgroud)
有人会理解为什么这些不再起作用吗?
| 归档时间: |
|
| 查看次数: |
2076 次 |
| 最近记录: |