将原有的
var app = builder.Build();
换成
var app = builder.Services.AddServices(builder);
public class UserService : ServiceBase {
public UserService() : base() {
App.MapGet("/api/weatherforecast", GetWeatherForecast);
}
public async Task<WeatherForecast[]> PostWeather() {
return null;
}
public async Task< WeatherForecast[]> GetWeatherForecast() {
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
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;
}
public async Task<IResult> Register() {
return Results.Ok("注册成功");
}
}
public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) {
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
提问:为什么我们只写了一个app.MapGet,却生成了三个接口
下一篇:MASA MinimalAPI源码解析:为什么我们只写了一个app.MapGet,却生成了三个接口MASA MinimalAPI源码解析:为什么我们只写了一个app.MapGet,却生成了三个接口