using Infrastructure.Data; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System.Text.Encodings.Web; using System.Text.Unicode; using TeacherExt.Data; namespace TeacherExt { public class Startup { private readonly string _origins = "AllowAllHeaders"; public Startup(IConfiguration configuration, IHostEnvironment hostEnvironment) { Configuration = configuration; HostEnvironment = hostEnvironment; } public IConfiguration Configuration { get; } public IHostEnvironment HostEnvironment { get; } public void ConfigureServices(IServiceCollection services) { services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All)); services.AddHttpContextAccessor(); services.AddCors(options => options.AddPolicy(_origins,builder => { builder.SetIsOriginAllowed(o => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); })); var connstr = HostEnvironment.IsDevelopment() ? Configuration.GetConnectionString("sqlite") : Configuration.GetConnectionString("mysql"); if (HostEnvironment.IsDevelopment()) { services.AddDbContext(o => o.UseSqlite(connstr)); } else { services.AddDbContext(o => o.UseMySql(connstr, ServerVersion.AutoDetect(connstr))); } services.AddScoped(); services.AddTransient(typeof(IRepository<>), typeof(EfRepository<>)); services.AddControllersWithViews(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseCors(_origins); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); using var scope = app.ApplicationServices.CreateScope(); using var db = scope.ServiceProvider.GetRequiredService(); if (db.Database.EnsureCreated()) { } } } }