Enabling a CORS policy in ASP.NET Core
Our goal in this section is to use an interface with CORS policy and configure it to allow any SPAs to send requests to our ASP.NET Core Web APIs.
So let's go to the Startup.cs
file of the Travel.WebApi
project and update the ConfigureServices
method by adding the following code under the AddSpaStaticFiles
method:
services.AddCors();
The services.AddCors
method will be the last method we are calling inside ConfigureServices
. This service is an extension method for setting up CORS services.
We are not yet done here; we still need to update our middleware. So, go to the Configure
method and add the following code under app.UseSpaStaticFiles
:
app.UseCors(b => { b.AllowAnyOrigin(); b.AllowAnyHeader(); b.AllowAnyMethod(); });
The UseCors
method adds a CORS middleware to our web application's pipeline...