Files
Web_AutoNotificatPhone_Serv…/Program.cs
2025-11-20 09:56:33 +08:00

43 lines
1.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using AutoNotificatPhone.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
// 添加 CORS 服务,配置允许任意来源访问
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin", builder =>
{
builder.AllowAnyOrigin() // 允许任何来源
.AllowAnyMethod() // 允许任何HTTP方法
.AllowAnyHeader(); // 允许任何请求头
});
});
builder.Services.AddHostedService<TimerClass>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// 生产环境建议使用更严格的CORS策略
// app.UseCors("RestrictedPolicy");
}
app.UseStaticFiles();
app.UseRouting();
// 使用 CORS 中间件(放在 UseRouting 之后UseAuthorization 之前)
app.UseCors("AllowAnyOrigin");
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();