Created
February 13, 2025 08:27
-
-
Save donma/319d370b65e5326dff92f41339714933 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Program | |
| { | |
| public static void Main(string[] args) | |
| { | |
| var builder = WebApplication.CreateBuilder(args); | |
| //新增 Cors 設定 | |
| builder.Services.AddCors(options => | |
| { | |
| options.AddPolicy("ALLOWALL", | |
| policy => policy.AllowAnyOrigin() // 允許任意 Origin | |
| .AllowAnyMethod() // 允許任意 HTTP Method (GET, POST, PUT, DELETE ..) | |
| .AllowAnyHeader()); // 允許任意 Headers | |
| }); | |
| // Add services to the container. | |
| builder.Services.AddRazorPages(); | |
| var app = builder.Build(); | |
| // Configure the HTTP request pipeline. | |
| if (!app.Environment.IsDevelopment()) | |
| { | |
| app.UseExceptionHandler("/Error"); | |
| // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | |
| app.UseHsts(); | |
| } | |
| app.UseHttpsRedirection(); | |
| app.UseStaticFiles(); | |
| app.UseRouting(); | |
| //這個要注意順序不然可能失效 | |
| app.UseCors("ALLOWALL"); // 啟用 CROS | |
| app.UseAuthorization(); | |
| app.MapRazorPages(); | |
| app.Run(); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment