在Startup文件的ConfigureServices函数里注入服务

  public void ConfigureServices(IServiceCollection services)
        {
           
            #region Cors跨域请求
            services.AddCors(c =>
            {
                c.AddPolicy("AllRequests", policy =>
                {
                    policy
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader();

                });
            });
            #endregion

            services.AddControllers();
        }

在其后的Configure函数中开启中间件

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

           
            //开启Cors跨域请求中间件
            app.UseCors("AllRequests");

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }

 

相关文章:

  • 2022-01-24
  • 2022-02-17
  • 2021-06-02
  • 2022-01-07
  • 2021-07-18
  • 2022-12-23
猜你喜欢
  • 2022-01-13
  • 2022-12-23
  • 2022-02-28
  • 2021-08-16
  • 2021-07-12
  • 2021-11-18
相关资源
相似解决方案