DEV Community

Mahir Xalilov
Mahir Xalilov

Posted on

PROG.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

builder.Services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();

builder.Services.AddDbContext(opt =>
{
opt.UseSqlServer(builder.Configuration.GetConnectionString("MsSql"));
opt.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
});

builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();

builder.Services.AddAutoMapper(typeof(PlaceProfile));
builder.Services.AddAutoMapper(typeof(CategoryProfile));

builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Admin", policy => policy.RequireRole("Admin"));
options.AddPolicy("User", policy => policy.RequireRole("User"));
});

builder.Services.AddAuthentication()
.AddCookie(options =>
{
options.AccessDeniedPath = "/Account/AccessDenied/";
});

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
try
{
await DatabaseSeeder.SeedData(scope.ServiceProvider);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
name : "areas",
pattern : "{area:exists}/{controller=Dashboard}/{action=Index}/{id?}"
);

app.MapControllerRoute(
name:"default",
pattern:"{controller=Home}/{action=Index}/{id?}"
);

app.Run();

Top comments (0)