如何使用c#前端reactJS允许POST和PUT方法使用CORS

问题描述 投票:-1回答:1

问题:在本地主机中,所有方法(GET,POST,PUT)都可以正常工作,但是在服务器中部署后,我得到了CORS POLICY BLOCKED ERr。尽管在服务器GET方法中工作正常

带有ef核心的C#跟随我的Startup.cs文件。

公共类启动{公共启动(IConfiguration配置){配置=配置;}

    readonly string AllowLocalHostOrigins = "_allowLocalHostOrigins";

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContextPool<venus_aestheticsContext>(options => options
           // replace with your connection string
           .UseMySql(Configuration["dbConnection:startup"],
               mysqlOptions =>
               {
                   mysqlOptions
                       .ServerVersion(new Version(8, 0, 18), ServerType.MySql);
               }));
        // services.AddScoped<IDataRepository<Token, long>, TokenManager>();
        // services.AddMvc();

        // Register the Swagger generator, defining 1 or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
        });

        services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
        services.AddControllers();

        // enable cors
        services.AddCors(options =>
        {
            options.AddPolicy(AllowLocalHostOrigins,
                builder =>
                {
                    builder.WithOrigins("http://localhost:3000").AllowAnyHeader().AllowAnyMethod();
                    builder.WithOrigins("http://www.testing.com").AllowAnyHeader().AllowAnyMethod();
                });
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        services.AddDistributedMemoryCache();
        services.AddSession(options =>
        {
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;
        });

        //     services.AddPaging();

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseOptions();
        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            //c.RoutePrefix = string.Empty;

        });

        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();
        app.UseSession();

        app.UseRouting();
        app.UseCors(AllowLocalHostOrigins);
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
c# reactjs post cors
1个回答
0
投票

您可以替换

app.UseCors(AllowLocalHostOrigins);

app.UseCors(builder => builder
   .AllowAnyOrigin()
   .AllowAnyMethod()
   .AllowAnyHeader()
   .AllowCredentials());
© www.soinside.com 2019 - 2024. All rights reserved.