public override void OnException(ExceptionContext filterContext) { if (filterContext.ExceptionHandled) { return; } var controllerName = (string)filterContext.RouteData.Values["controller"]; var actionName = (string)filterContext.RouteData.Values["action"]; var request = filterContext.HttpContext.Request; LogHelper.Logger.Fatal(filterContext.Exception, $"【异常信息】:{filterContext.Exception.Message} 【请求路径】:{request.Method}:{request.Path}\n " + $"【控制器】:{controllerName} - 【方法】:{actionName}\n " + $"【主机地址】:{ DemoWeb.GetClientIp()} " + $"【用户代理】:{ request.Headers["User-Agent"]}");//DemoWeb为前面章节添加的类 if (filterContext.Exception is CustomSystemException se) { filterContext.Result = new CustomHttpStatusCodeResult(200, se.Code, se.Message); } else { #if DEBUG Console.WriteLine(filterContext.Exception); var content = filterContext.Exception.ToJson();//ToJson为静态扩展方法 为前面章节添加的类 #else var content = "系统错误,请稍后再试或联系系统管理人员。"; #endif filterContext.Result = new CustomHttpStatusCodeResult(200, 500, content);//CustomHttpStatusCodeResult为自定义返回结果 为前面章节添加的类 } filterContext.ExceptionHandled = true; }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMemoryCache memoryCache) { DemoWeb.IocManager = app.ApplicationServices.GetService <IIocManager>(); DemoWeb.Environment = env; try//注意这里在本地开发允许时会重置数据库,并清空所有数据,如不需要请注释 在不存在数据库的时候请将这个地方的注释打开 { if (env.IsDevelopment()) { using (var servicescope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>() .CreateScope()) { //var dbcontent = servicescope.serviceprovider.getservice<demodbcontext>(); //checkmigrations(dbcontent); var database = servicescope.ServiceProvider.GetService <DemoDbContext>().Database; database.EnsureDeleted(); database.EnsureCreated(); } } } catch (Exception ex) { LogHelper.Logger.Error(ex, "Failed to migrate or seed database"); } DemoWeb.Configure(app.ApplicationServices.GetRequiredService <IHttpContextAccessor>()); DemoWeb.MemoryCache = memoryCache; if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());//允许跨域 app.UseHttpsRedirection(); // Fetch errorInternal Server Error v1/swagger.json 错误 有方法未指明请求方式 GET POST #region Swagger app.UseSwagger(c => { c.RouteTemplate = "apidoc/{documentName}/swagger.json"; }); app.UseSwaggerUI(c => { c.RoutePrefix = "apidoc"; c.SwaggerEndpoint("v1/swagger.json", "ContentCenter API V1"); c.DocExpansion(DocExpansion.None);//默认文档展开方式 }); #endregion app.UseMvc(routes => { routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); }); //app.UseMvc(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMemoryCache memoryCache) { DemoWeb.IocManager = app.ApplicationServices.GetService <IIocManager>(); DemoWeb.Environment = env; try//注意这里在本地开发允许时会重置数据库,并清空所有数据,如不需要请注释 在不存在数据库的时候请将这个地方的注释打开 { if (env.IsDevelopment()) { using (var servicescope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>() .CreateScope()) { //var dbcontent = servicescope.serviceprovider.getservice<demodbcontext>(); //checkmigrations(dbcontent); var database = servicescope.ServiceProvider.GetService <DemoDbContext>().Database; database.EnsureDeleted(); database.EnsureCreated(); } } } catch (Exception ex) { LogHelper.Logger.Error(ex, "Failed to migrate or seed database"); } DemoWeb.Configure(app.ApplicationServices.GetRequiredService <IHttpContextAccessor>()); DemoWeb.MemoryCache = memoryCache; if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseCors(builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());//允许跨域 app.UseHttpsRedirection(); app.UseMvc(routes => { routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); }); //app.UseMvc(); }