Я создал бэкэнд Net Core API, который работает в VS и Postman.Однако при создании клиентского интерфейса Angular CLI я получаю эту ошибку.Это не имеет смысла, так как я включил Add Cors для AllowAnyOrigin ниже.
Угловая ошибка:
Access to XMLHttpRequest at 'https://localhost:xxxx/api/values' from origin 'http://localhost:xxxx' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddCors((options =>
{ options.AddPolicy("AllowAllOrigins", builder => builder.AllowAnyOrigin());}));
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors();
// Shows UseCors with CorsPolicyBuilder.
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.2