我已经使用Core 2.0创建了一个Web API,并且在启用启用了cors的跨域调用时,收到以下错误:“所请求的资源上没有'Access-Control-Allow-Origin' header 。”
以下是我在startup.cs中的配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging();
services.AddMvc();
services.AddCors(options =>
{
options.AddPolicy("AllowAll",
builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new CorsAuthorizationFilterFactory("AllowAll"));
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
app.UseCors("AllowAll");
}
根据我在stackoverflow或其他地方看到的每篇帖子/教程,这是允许所有来源的正确方法。我想念什么?
最佳答案
尝试调用:app.UseCors("AllowAll");
,然后再调用 app.UseMvc();
。
documentation声明以下内容:
To enable CORS for your entire application add the CORS middleware to your request pipeline using the UseCors extension method. Note that the CORS middleware must precede any defined endpoints in your app that you want to support cross-origin requests (ex. before any call to UseMvc).
关于asp.net - .NET Core 2.0中的CORS "No ' Access-Control-Allow-Origin header 出现在所请求的资源上。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48649878/