|
| 1 | +using Microsoft.Net.Http.Headers; |
| 2 | + |
| 3 | +var builder = WebApplication.CreateBuilder(args); |
| 4 | + |
| 5 | +const string policyName = "CorsPolicy"; |
| 6 | +const string anotherPolicyName = "AnotherCorsPolicy"; |
| 7 | + |
| 8 | +builder.Services.AddCors(options => |
| 9 | +{ |
| 10 | + options.AddPolicy(name: policyName, builder => |
| 11 | + { |
| 12 | + builder.WithOrigins("https://localhost:5011") |
| 13 | + .AllowAnyHeader() |
| 14 | + .WithMethods("GET"); |
| 15 | + }); |
| 16 | + options.AddPolicy(name: anotherPolicyName, builder => |
| 17 | + { |
| 18 | + builder.WithOrigins("https://localhost:5021") |
| 19 | + .WithHeaders(HeaderNames.ContentType, HeaderNames.Accept) |
| 20 | + .WithMethods("GET") |
| 21 | + .WithExposedHeaders("X-Pagination"); |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +builder.Services.AddControllers(); |
| 26 | + |
| 27 | +builder.Services.AddOpenApi(); |
| 28 | +builder.Services.AddProblemDetails(); |
| 29 | + |
| 30 | +var app = builder.Build(); |
| 31 | + |
| 32 | +if (app.Environment.IsDevelopment()) |
| 33 | +{ |
| 34 | + app.MapOpenApi(); |
| 35 | + app.UseDeveloperExceptionPage(); |
| 36 | +} |
| 37 | +else |
| 38 | +{ |
| 39 | + app.UseExceptionHandler(); |
| 40 | + app.UseHsts(); |
| 41 | + app.UseStatusCodePages(); |
| 42 | +} |
| 43 | + |
| 44 | +app.UseHttpsRedirection(); |
| 45 | + |
| 46 | +app.UseCors(); |
| 47 | + |
| 48 | +app.UseAuthentication(); |
| 49 | +app.UseAuthorization(); |
| 50 | + |
| 51 | +app.MapControllers(); |
| 52 | + |
| 53 | +var summaries = new[] |
| 54 | +{ |
| 55 | + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" |
| 56 | +}; |
| 57 | + |
| 58 | +app.MapGet("/minimal/weatherforecast", () => |
| 59 | + { |
| 60 | + var forecast = Enumerable.Range(1, 5).Select(index => |
| 61 | + new WeatherForecast |
| 62 | + ( |
| 63 | + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), |
| 64 | + Random.Shared.Next(-20, 55), |
| 65 | + summaries[Random.Shared.Next(summaries.Length)] |
| 66 | + )) |
| 67 | + .ToArray(); |
| 68 | + return forecast; |
| 69 | + }) |
| 70 | + .WithName("GetWeatherForecast") |
| 71 | + .RequireCors(policyName); |
| 72 | + |
| 73 | +app.Run(); |
| 74 | + |
| 75 | +public record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) |
| 76 | +{ |
| 77 | + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); |
| 78 | +} |
0 commit comments