.net core 从本地主机连接到服务器API时升级到DotNet 3.1后出现CORS问题
当我尝试从本地系统(localhost:4200)连接到托管在IIS上的API(DotNet Core 3.1)时,在浏览器中将项目迁移到DotNet Core 3.1后,我遇到以下错误 访问位于“”的XMLHttpRequesthttps://dev.ncop.firstam.net/multisiteservice/api/v1/test/method“起源”http://localhost:4200'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'access control Allow Origin'标头 这是mystartup.cs中的CORS代码片段.net core 从本地主机连接到服务器API时升级到DotNet 3.1后出现CORS问题,.net-core,cors,.net-core-3.1,.net Core,Cors,.net Core 3.1,当我尝试从本地系统(localhost:4200)连接到托管在IIS上的API(DotNet Core 3.1)时,在浏览器中将项目迁移到DotNet Core 3.1后,我遇到以下错误 访问位于“”的XMLHttpRequesthttps://dev.ncop.firstam.net/multisiteservice/api/v1/test/method“起源”http://localhost:4200'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'acces
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
var allowedOrigin = Configuration["AppSettings:CorsAllowedOrigin"];
{
options.AddPolicy("SiteCorsPolicy",
builder => builder.AllowAnyOrigin()
.WithOrigins(allowedOrigin)
.AllowAnyMethod()
.AllowCredentials()
.AllowAnyHeader());
}
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
{
app.UseCors("SiteCorsPolicy");
app.UseAuthentication();
app.UseMvc();
//Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
}
这是我的应用程序设置文件:
"AppSettings": {
"CorsAllowedOrigin": "http://localhost:4200/,https://dev.ncop.fistam.com/,https://staging.ncop.fistam.com/,https://ncop.fistam.com,http://azuvnintfint551.fastts.firstam.net,https://staging.webservices.firstam.net
}
如何从本地系统连接到服务器API?我错过了什么。
根据
URL不能包含尾随斜杠(/)。如果URL终止
使用/,比较返回false,不返回任何头
AppSettings
中的域不应包含尾部正斜杠(/
)
你能用这个试试吗:
"AppSettings": {
"CorsAllowedOrigin": "http://localhost:4200,https://dev.ncop.fistam.com,https://staging.ncop.fistam.com,https://ncop.fistam.com,http://azuvnintfint551.fastts.firstam.net,https://staging.webservices.firstam.net
}