欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.Net Core2.0允许跨域请求设置

程序员文章站 2022-07-07 15:32:41
1、services 2、app ......

 

1、services

 1         /// <summary>
 2         /// 
 3         /// </summary>
 4         /// <param name="services"></param>
 5         public void ConfigureServices(IServiceCollection services)
 6         {
 7             #region 允许跨域请求
 8             services.AddCors(options =>
 9             {
10                 options.AddPolicy("CorsPolicy",
11                     builder => builder
12                         .AllowAnyOrigin()
13                         .AllowAnyMethod()
14                         .AllowAnyHeader()
15                         .AllowCredentials());
16             });
17             #endregion
18          }

2、app

 1         /// <summary>
 2         /// 
 3         /// </summary>
 4         /// <param name="app"></param>
 5         /// <param name="env"></param>
 6         /// <param name="loggerFactory"></param>
 7         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
 8         {
 9             #region 允许跨域请求
10             app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
11             #endregion
12         }