title: Enabling CORS in Google App Engine Flexible Environment
body: <p>I am facing issues enabling CORS support for a ASP.NET CORE application that is hosted using Google App Engine and the flexible environment.</p>
  
  <p>Every AJAX request using the axios library results in the following error...</p>
  
  <blockquote>
    <p>Access to XMLHttpRequest at '<a href="https://api.[something].services/request" rel="nofollow noreferrer">https://api.[something].services/request</a>'
    from origin '<a href="http://localhost:8080" rel="nofollow noreferrer">http://localhost:8080</a>' has been blocked by CORS policy:
    No 'Access-Control-Allow-Origin' header is present on the requested
    resource.</p>
  </blockquote>
  
  <p>Here the configuration of CORS on the web api:</p>
  
  <pre class="lang-c# prettyprint-override"><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddCors(options =&gt; 
      options.AddPolicy("MyPolicy", 
          builder =&gt;
          {
              builder.AllowAnyOrigin()
              .AllowAnyMethod()
              .AllowAnyHeader()
              .AllowCredentials();
          }));
  
     // ...
  
     services.AddMvc();
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  {
      if (env.IsDevelopment())
      {
          app.UseDeveloperExceptionPage();
  
          System.Environment
                .SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS",
                            Path.Combine(Directory.GetCurrentDirectory(),
                            Configuration["GAE:Credentials"]));
      }
      else
      {
          app.UseHsts();
      }
  
      // ...
  
      app.UseCors("MyPolicy");
      app.UseHttpsRedirection();
      app.UseMvc();
  }
  </code></pre>