@fasadin: Dodałem app.UseCors(.....);
oraz var services = builder.Services; services.AddCors();
i dalej ten sam błąd.
Kod Program.cs:
var builder = WebApplication.CreateBuilder(args);
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new RestaurantMappingProfile());
});
var services = builder.Services;
services.AddCors();
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllers().AddFluentValidation();
builder.Services.AddAutoMapper(typeof(RestaurantMappingProfile));
builder.Services.AddSingleton(mapperConfig.CreateMapper());
builder.Services.AddDbContext<RestaurantDbContext>(options => options.UseSqlServer("name=ConnectionStrings:DefaultConnection"));
builder.Services.AddScoped<IRestaurantService, RestaurantService>();
builder.Services.AddScoped<IDishService, DishService>();
builder.Services.AddScoped<AuthenticationSettings>();
builder.Services.AddScoped<IAccountService, AccountService>();
builder.Services.AddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
builder.Services.AddScoped<IValidator<RegisterUserDto>, RegisterUserDtoValidator>();
builder.Host.UseNLog();
builder.Services.AddScoped<ErrorHandlingMiddleware>();
var app = builder.Build();
// configure HTTP request pipeline
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.Use(async (context, next) =>
{
// Do work that doesn't write to the Response.
await next.Invoke();
// Do logging or other work that doesn't write to the Response.
});
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseMiddleware<ErrorHandlingMiddleware>();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapRazorPages();
app.MapControllers();
app.MapControllerRoute
(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"
);
app.UseEndpoints(x =>
{
x.MapGet("/ping", () => "pong");
});
app.Run(async context =>
{
await context.Response.WriteAsync("Hello from 2nd delegate.");
});
app.UseMiddleware<RequestMiddleware>();
app.Run();