title: Access to XMLHttpRequest from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource
body: <p>I have created an API project in MVC Core. In my controller I have added some APIs of GET and POST methods that work perfectly fine with Postman. But when I try calling them from my Angular App, they give me CORS error:</p>
  
  <blockquote>
    <p>Access to XMLHttpRequest from origin has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource</p>
  </blockquote>
  
  <p>I googled for solutions and found out that I needed to add CORS NuGet package. I did that, but the error is still there.</p>
  
  <p>Following is my <code>Startup.cs</code> file code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.HttpsPolicy;
  using Microsoft.AspNetCore.Mvc;
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using Microsoft.Extensions.Logging;
  using webapp1.Model;
  
  namespace webapp1
  {
      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.AddControllers();
  
              services.AddCors(options =&gt;
              {
                  options.AddPolicy("AllowAnyOrigin",
                      builder =&gt; builder
                      .AllowAnyOrigin()
                      .AllowAnyMethod()
                      .AllowAnyHeader());
              });
  
              services.AddDbContext&lt;TodoContext&gt;(options =&gt; options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
              }
  
              app.UseHttpsRedirection();
  
              app.UseRouting();
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllers();
              });
  
              app.UseCors(options =&gt;
              options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
  
          }
      }
  }
  
  </code></pre>
  
  <p>And following is my <code>API Controller</code>:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using webapp1.Model;
  
  namespace webapp1.Controllers
  {
      [ApiController]
      [Route("[controller]")]
      public class TodoController : ControllerBase
      {
          TodoContext _context;
  
          public TodoController(TodoContext context)
          {
              _context = context;
          }
  
          [HttpGet]
          public List&lt;Todo&gt; Get()
          {
              return _context.Todos.ToList();
          }
      }
  }
  
  </code></pre>