Skip to content

Instantly share code, notes, and snippets.

@donma
Created February 13, 2025 08:27
Show Gist options
  • Select an option

  • Save donma/319d370b65e5326dff92f41339714933 to your computer and use it in GitHub Desktop.

Select an option

Save donma/319d370b65e5326dff92f41339714933 to your computer and use it in GitHub Desktop.
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