public Startup(IConfiguration configuration, IHostEnvironment env) { GlobalTo.Configuration = configuration; GlobalTo.HostEnvironment = env; //无创建,有忽略 using var db = new Data.ContextBase(); //不存在创建,创建后返回true if (db.Database.EnsureCreated()) { //调用重置数据库(实际开发中,你可能不需要,或只初始化一些表数据) new Controllers.ToolController().ResetDataBaseForJson(); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, Data.ContextBase db, IMemoryCache memoryCache) { //缓存 Core.CacheTo.memoryCache = memoryCache; //是开发环境 if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } //数据库不存在则创建,创建后返回true if (db.Database.EnsureCreated()) { //调用重置数据库(实际开发中,你可能不需要,或只初始化一些表数据) new Controllers.ToolController(db).ResetDataBaseForJson(); } //配置swagger(生产环境不需要,把该代码移至 是开发环境 条件里面) app.UseSwagger().UseSwaggerUI(c => { c.DocumentTitle = "RF API"; c.SwaggerEndpoint("/swagger/v1/swagger.json", c.DocumentTitle); }); //默认起始页index.html DefaultFilesOptions options = new DefaultFilesOptions(); options.DefaultFileNames.Add("index.html"); app.UseDefaultFiles(options); //静态资源允许跨域 app.UseStaticFiles(new StaticFileOptions() { OnPrepareResponse = (x) => { x.Context.Response.Headers.Add("Access-Control-Allow-Origin", "*"); } }); app.UseRouting(); //授权访问 app.UseAuthentication(); app.UseAuthorization(); //session app.UseSession(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }