.NET Core Web API cross-domain requests

Built using .net core 3.0 Web API interface to access when using another server reported this error:

 

 

Solutions are as follows:

1, the following were added in the startup method ConfigureServices class web api project.

        /// <summary>
        /// 
        /// </summary>
        /// <param name="services"></param>
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            //
            services.AddCors(options =>
            {
                options.AddPolicy("AllowSpecificOrigins",
                    builder =>
                    {
                        builder.WithOrigins("http://localhost:56003").AllowAnyHeader();
                    });
            });
        }    
 

2. Add the following in the configure method, the recompilation to run on it.

        /// <summary>
        /// 
        /// </summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            
            app.UseCors("AllowSpecificOrigins");
            
            app.UseStaticFiles();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }        

  

Guess you like

Origin www.cnblogs.com/sjt072/p/11929084.html