43 lines
1.0 KiB
C#
43 lines
1.0 KiB
C#
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(); |