title: How to enable cors in ASP.NET Core 6.0 Web API project?
body: <p>Configured CORS in my ASP.NET Core 6.0 Web API project. But the preflight request receives a http 405 error.</p>
  <p>In other words HTTP OPTION is not allowed. Looks like cors is not enabled.</p>
  <p>I've seen examples with <code>config.EnableCors();</code> but there is no <code>App_Start/WebApiConfig.cs</code> in this project template.</p>
  <p>What am I missing here?</p>
  <p>Program.cs</p>
  <pre><code>var builder = WebApplication.CreateBuilder(args);
  
  // Add services to the container.
  
  builder.Services.AddControllers();
  
  // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  builder.Services.AddEndpointsApiExplorer();
  builder.Services.AddSwaggerGen();
  
  var devCorsPolicy = &quot;devCorsPolicy&quot;;
  builder.Services.AddCors(options =&gt;
  {
      options.AddPolicy(devCorsPolicy, builder =&gt; {
          //builder.WithOrigins(&quot;http://localhost:800&quot;).AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
          builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
          //builder.SetIsOriginAllowed(origin =&gt; new Uri(origin).Host == &quot;localhost&quot;);
          //builder.SetIsOriginAllowed(origin =&gt; true);
      });
  });
  
  
  var app = builder.Build();
  
  if (app.Environment.IsDevelopment())
  {
      app.UseSwagger();
      app.UseSwaggerUI();
      app.UseCors(devCorsPolicy);
  }
  else 
  {
      app.UseHttpsRedirection();
      app.UseAuthorization();
      //app.UseCors(prodCorsPolicy);
  }
  
  app.MapControllers();
  
  app.Run();
  </code></pre>