I have ASP.NET Core API and application in Angular 5. I have added code in Startup.cs to enable cors and to allow all origin, but still, it is giving me an error that
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 500.
Below is my web api code:
public void ConfigureServices(IServiceCollection services)
{
WriteToFile("In configure service");
try
{
// Enable Cors
services.AddCors();
services.AddMvc();
}
catch
{
WriteToFile("Not Connected in StartUP");
}
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
try
{
app.UseCors(builder =>
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod());
app.UseMvc();
WriteToFile("After cors use In configure");
}
catch(Exception ex)
{
WriteToFile("Error in configure"+ex.Message);
}
}