吴晓阳
发布于 2025-04-13 / 5 阅读
0

让.net9.0生成的默认网站支持swagger

让.net9.0生成的默认网站支持swagger

.net9.0生成的默认网站,默认不支持swagger

我们要安装Swashbuckle.AspNetCore.SwaggerUI

app.UseSwaggerUI(options  =>
{
     options.SwaggerEndpoint("/openapi/v1.json",  "v1");
});
using WebApplication1;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.  

builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi  
builder.Services.AddOpenApi();
//builder.Services.AddSwaggerGen();

// Fix: Use the correct overload for AddMediatR  
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssemblyContaining<GreetingRequestHandler>());


var app = builder.Build();

// Configure the HTTP request pipeline.  
//if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
    //app.UseSwagger();
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/openapi/v1.json", "v1");
    });
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();