mirror of
https://github.com/hubHarmony/Csharp-API-Template.git
synced 2024-11-17 21:40:31 +00:00
53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
|
var builder = WebApplication.CreateBuilder(args);
|
||
|
|
||
|
|
||
|
builder.Services.AddEndpointsApiExplorer();
|
||
|
builder.Services.AddSwaggerGen();
|
||
|
builder.Services.AddCors(options =>
|
||
|
{
|
||
|
options.AddPolicy("AllowAllOrigins", corsBuilder =>
|
||
|
{
|
||
|
corsBuilder.AllowAnyOrigin()
|
||
|
.AllowAnyMethod()
|
||
|
.AllowAnyHeader();
|
||
|
});
|
||
|
});
|
||
|
|
||
|
var app = builder.Build();
|
||
|
|
||
|
|
||
|
if (app.Environment.IsDevelopment())
|
||
|
{
|
||
|
app.UseSwagger();
|
||
|
app.UseSwaggerUI();
|
||
|
}
|
||
|
|
||
|
app.UseHttpsRedirection();
|
||
|
app.UseCors("AllowAllOrigins");
|
||
|
|
||
|
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")
|
||
|
.WithOpenApi();
|
||
|
|
||
|
app.Run();
|
||
|
|
||
|
internal record WeatherForecast (DateOnly Date, int TemperatureC, string? Summary)
|
||
|
{
|
||
|
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||
|
}
|