在静态类中写一个扩展方法
/// <summary>
/// 跨域解决方案
/// </summary>
/// <param name="Services"></param>
public static void AddCorsExt(this IServiceCollection Services)
{
Services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", opt => opt.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
}
在主函数Program.cs
builder.Services.AddCorsExt()