WTM(ASP.NET Core)跨域设置

使用WTM小伙伴们可能都会向我一样多少都会遇到跨域的问题。明明在appsettings.json中CorsOptions设置EnableAll为true,可是在调试SPA应用的时候还是显示有跨域的错误提示。解决办法如下:

一、 ConfigureServices方法增加内容

 x.AddCors(options =>
                        {
                            options.AddPolicy("all", builder =>
                            {
                                builder.SetIsOriginAllowed(_ => true) //允许任何来源的主机访问
                                .AllowAnyMethod()
                                .AllowAnyHeader()
                                .AllowCredentials();//指定处理cookie
                            });
                        });

二、在Configure方法中添加 

x.UseCors("all");

这样在重启项目,就实现了WTM的跨域。 

X、如果需要限定访源,则ConfigureServices修改 

 services.AddCors(options => options.AddPolicy("CorsPolicy",
            builder =>
            {
                builder.WithOrigins(new string[] { "http://127.0.0.1:5500" })
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
            }));

猜你喜欢

转载自blog.csdn.net/sxy_student/article/details/107885434