参考链接:
博客园: 在.NET 9中使用Scalar替代Swagger
MSDoc: Use openAPI documents
.NET 9 刚刚发布,默认的 API 模板里,老朋友 Swagger UI 被砍了(好刀法🙄),不过 OpenAPI 的接口文档还在,导入第三方啥的倒是没啥问题。
配合 ApiExplorer 和 .http
文件的内置支持,倒也可以实现交互式的接口测试。
不过老朋友 1 秒不见,甚是想念,不习惯的还是能找回来滴。
.NET9 WebAPI 默认模板
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.Run();
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
API 调用
{baseAddress}/weatherforecast
OpenAPI 接口
{baseAddress}/openapi/v1.json
Swagger UI
找回老朋友的步骤很简单啊:
- 安装
Swashbuckle.AspNetCore.SwaggerUi
包
Install-Package Swashbuckle.AspNetCore.SwaggerUi
- 配置使用
SwaggerUI
// 省略……
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
// 使用 Swagger UI
app.UseSwaggerUI(options => { options.SwaggerEndpoint("/openapi/v1.json", "v1"); });
}
// 省略……
- 访问 Swagger 路径(嘿呦,我的老朋友😎)
{baseAddress}/swagger/index.html
Scalar
同时,微软也推荐了另一个替代产品:Scalar
,给我感觉更像是简化版的 Postman
。使用步骤也很简单啊:
- 安装
Scalar.AspNetCore
包。
Install-Package Scalar.AspNetCore
- 配置使用
Scalar
// 省略……
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
// 使用 Swagger UI
app.UseSwaggerUI(options => { options.SwaggerEndpoint("/openapi/v1.json", "v1"); });
// 使用 Scalar
app.MapScalarApiReference();
}
// 省略……
- 访问 Scalar 的路径(嘿嘿,我的新朋友😎)
{baseAddress}/scalar/v1
不得不说,跟 Swagger 的配置简直一模一样啊。
完整代码
最后,奉上完整代码
using Scalar.AspNetCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.UseSwaggerUI(options => { options.SwaggerEndpoint("/openapi/v1.json", "v1"); });
app.MapScalarApiReference();
}
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast");
app.Run();
internal record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Comments