VStudio .NET Core with VSCode Angular - CORS issue

I developer a small web API related to catalogs management (most of my projects include this part). The Backend is a .NET Core + EF6 solution with a MSSQL database, in the other hand I have a front end project with Angular. Of course, both projects use different ports.

My backend is developed using VStudio 2022 and my front end is an independient project developed using VSCode.

I started creating my components and a service that connects with my API, but I have had CORS issues. How did I solve them?

In my backend I opened the Program.cs file and added before var app = builder.Build() the following code

builder.Services.AddCors(options => {
    options.AddPolicy(name: "AllowOrigin",
        builder => {
            builder.WithOrigins("http://localhost:4200")
            .AllowAnyHeader()
            .AllowAnyMethod();
        });
});

After that, and because I'm in development environment, I put this inside the if (app.Environment.IsDevelopment()): app.UseCors("AllowOrigin");

With this My front-end could retrieve the information from the backend.

Comentarios

Entradas populares