![](/img/trans.png)
[英]Error on Enable CORS in ASP.net Core 3.1 WebAPI in only when published in IIS
[英]CORS in ASP.NET Core in IIS
我发现了有趣的问题。 我在 Z5DA5ACF461B4EFB7E76EC861065B2126 上部署了我的 ASP.NET Core REST API
Access to XMLHttpRequest at 'https...' from origin 'https:...' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.
Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https:'.
但它在几分钟前就开始工作了。 我检查了响应 header ,这是在那里:
access-control-allow-headers: content-type,odata-maxversion,odata-version
access-control-allow-methods: GET, GET,POST,PUT,DELETE,OPTIONS
access-control-allow-origin: *, *
然后我意识到我重新启动了服务。 出于某种原因,每次上传服务时,我的代码和 IIS 重复 HTTP 响应 header 值。 当我删除 IIS 中的 HTTP 响应 Header 值时,它可以工作。 这是我的 Startup.cs 代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("AllPolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
services.AddControllers();
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseCors("AllPolicy");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
您是否知道为什么每次上传服务时 header 值都会重复? 你有任何想法如何解决它吗?
尝试移动“app.UseCors("AllPolicy");” 在 app.UseRouting(); 之后使它看起来像:
app.UseRouting();
app.UseCors("AllPolicy");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.