© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
8 replies
uselessxp

❔ WebAPI Cors problem when calling API from Frontend

Hi, I'm having the following code while trying calling my API from Frontend:
Access to XMLHttpRequest at 'http://localhost:5222/api/Users' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Access to XMLHttpRequest at 'http://localhost:5222/api/Users' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

For try to solve this I added a code in the
Program.cs
Program.cs
with an exception, but I'm still having the same problem and I don't know in which other way could I solve.
I show you the code of
Program.cs
Program.cs

using MyApp.RestAPI.Models.DataLayer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

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();

// Load configuration from appsettings.json
var configuration = builder.Configuration;

// Configure services.
builder.Services.AddDbContext<DimmiDevContext>(options =>
{
    options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
});

// Configure the HTTP request pipeline.
var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseCors(builder =>
{
    builder.WithOrigins("http://localhost:4200");
    builder.AllowAnyMethod();
    builder.AllowAnyHeader();
});

app.MapControllers();

app.Run();
using MyApp.RestAPI.Models.DataLayer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

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();

// Load configuration from appsettings.json
var configuration = builder.Configuration;

// Configure services.
builder.Services.AddDbContext<DimmiDevContext>(options =>
{
    options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
});

// Configure the HTTP request pipeline.
var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseRouting();

app.UseAuthorization();

app.UseCors(builder =>
{
    builder.WithOrigins("http://localhost:4200");
    builder.AllowAnyMethod();
    builder.AllowAnyHeader();
});

app.MapControllers();

app.Run();

I also tried to move the
app.UseCors
app.UseCors
between
app.UseRouting()
app.UseRouting()
and
app.UseAuthorization()
app.UseAuthorization()
with no results
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Frontend unable to connect to webapi when starting server manually (dotnet run)
C#CC# / help
3y ago
❔ Recommended Session Approach for WebApplication (React frontend, WebApi backend)
C#CC# / help
4y ago
✅ Issues logging in from frontend via backend API
C#CC# / help
2y ago