// 1.跨域配置 注册策略
services.AddCors(options => {
options.AddPolicy("CorsPolicy", builder =>
{
builder.SetIsOriginAllowed((x) => true)
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
// 2. 添加使用注册的策略
app.UseCors("CorsPolicy");
第二种 方式:添加自定义中间件进行配置
1. 添加中间件
/// <summary>
/// /自定义跨域配置中间件
/// </summary>
public class OptionsMiddleware
{
private readonly RequestDelegate _next;
public OptionsMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
return BeginInvoke(context);
}
p