乏mily

导航

.net core 3.1 中 的跨域设置

1、ConfigureServices中添加

           //添加cors 服务 配置跨域处理
            services.AddCors(options =>
            {
                options.AddPolicy("any", builder =>
                {
                    builder.SetIsOriginAllowed(_ => true)
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();//指定处理cookie
                });
            });

2、Configure中添加“app.UseCors()”:

            
app.UseHttpsRedirection(); app.UseRouting(); app.UseCors();
//配置Cors 注意这行代码的位置顺序,不然容易出错 app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); });

3、控制器类中添加特性:

 

posted on 2020-09-02 17:03  乏mily  阅读(233)  评论(0编辑  收藏  举报