CORS Latest Release .NET

CY 1 Reputation point
2024-09-16T13:44:24.3566667+00:00

I have created a new web api project using the latest .net release(.net 8) and I am getting the following error in my angular project.

Http failure response for "url": 0 Unknown Error

see my program.cs below:


var builder = WebApplication.CreateBuilder(args);

var Origin = "Origin";

var AllowedOrigins = builder?.Configuration?.GetValue<string>("AllowedOrigins")?.Split(',');

if (AllowedOrigins != default)

{

builder.Services.AddCors(options => { options.AddPolicy(Origin, policy =>

              {

                  policy.WithOrigins(AllowedOrigins)

                  .AllowAnyHeader()

                  .AllowAnyMethod();

              });

}); }

builder.Services.Configure<ConfigurationSetting>(builder.Configuration.GetSection("ConfigurationSetting"));

builder.Services.AddScoped<IMyObject, MyObject>();

// Add services to the container.

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle

builder?.Services.AddEndpointsApiExplorer();

builder?.Services.AddSwaggerGen();

builder?.Services.AddControllers();

var app = builder?.Build();

app.UseHttpsRedirection();

app.UseRouting();

app.UseCors(Origin);

app.UseStaticFiles();

app.UseAuthorization();

app.MapControllers();

// Configure the HTTP request pipeline.

if (app.Environment.IsDevelopment()) { app.UseSwagger();

app.UseSwaggerUI(); }

app.Run();

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,612 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,706 Reputation points
    2024-09-16T16:11:03.9+00:00

    use the browsers debug tools to see the full url and network response. also if CORS, there should of been a message in the browser console.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.