Ngu*_*han 16 asp.net-web-api .net-6.0
我正在使用.NET 6。
我正在为我的 Web API 核心进行版本控制。当我在 Program.cs 中实现 IApiVersionDescriptionProvider 时,我陷入了困境。
因为在创建新项目ASP.NET Core Web API时,只有program.cs文件。(旧的.NET Core有Startup.cs和Program.cs)
请帮助我,谢谢大家这是我的代码
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;
using VfcSolution.Application.User;
using VfcSolution.WebApi;
using VfcSolution.WebApi.VfcSolutionMapper;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped<IUserRepository, OracleUserRepository>();
builder.Services.AddAutoMapper(typeof(VfcSolutionMappings));
builder.Services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ReportApiVersions = true;
});
builder.Services.AddVersionedApiExplorer(options => options.GroupNameFormat = "'v'VVV");
builder.Services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
builder.Services.AddSwaggerGen();
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
if(app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI(options =>
{
==> I'M STUCKED IN HERE, I DO NOT KNOW HOW TO IMPLEMENT IApiVersionDescriptionProvider
foreach (var desc in provider.ApiVersionDescriptions)
{
}
});
app.UseAuthorization();
app.MapControllers();
app.Run();
Run Code Online (Sandbox Code Playgroud)
小智 30
您需要在 ; 之后添加以下行builder.Build():
using Microsoft.AspNetCore.Mvc.ApiExplorer;
var app = builder.Build();
var provider = app.Services.GetRequiredService<IApiVersionDescriptionProvider>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11652 次 |
| 最近记录: |