You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/labs/Teacher/TeacherExt/Startup.cs

81 lines
2.8 KiB

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<TeacherDbContext>(o => o.UseSqlite(connstr));
}
else
{
services.AddDbContext<TeacherDbContext>(o => o.UseMySql(connstr, ServerVersion.AutoDetect(connstr)));
}
services.AddScoped<DbContext, TeacherDbContext>();
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<DbContext>();
if (db.Database.EnsureCreated())
{
}
}
}
}