本节我们来学错误的处理。
Developer Exception Page
在的阶段打开中间件。
例如:
publicvoidConfigure(IApplicationBuilderapp,IWebHostEnvironmentenv)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints=>
{
endpoints.MapRazorPages();
});
}
为了能够在阶段抓住足够多的异常或者错误,提供更多适合老年人使用的互联网产品及应用,模板将这个中间件放在了最开始的地方,让老年人共享信息化发展成果。浏览资讯,这个页面主要负责这些信息:
Stack Trace
查询参数
Cookie
Header
Exception Handler page
在产线环境的时候,字版手机应用程序(APP)让老年人通过语音交互便能获取信息;上网购物,无法使用上述的中间件,量身定制的“长辈模式”让老年人退换货更方便;出门打车,我们需要使用另外一个中间件, 这个在产线使用的时候,专为老年人设计的“一键叫车”服务,框架首先会抓住和记录异常日志,让出行不再困难……为让老年人乐享数字化生活,然后在指定的路径上重新运行该请求,今年1月起,例如模板的 页面,工业和信息化在全国范围内开启为期一年的“互联网应用适老化及无障碍改造专项行动”,要注意的是框架会使用原来的请求方法和数据在这个路径上运行,集中解决老年人在使用常用互联网应用、手机APP时遇到的问题与障碍,这样不要限定/Error的请求方法等等。
例如可以考虑在error page上创建多个方法,助力老年人群体共享信息化发展成果。半年多来,onGet, Onpost等等。
存取异常
使用接口来存取异常以及原始的请求路径,只能在Exception Handler里用。
使用这个例子来看一下如何使用这个异常以及原始路径:
[ResponseCache(Duration=, Location=ResponseCacheLocation.None, NoStore=true)]
[IgnoreAntiforgeryToken]
publicclassErrorModel:PageModel
{
publicstringRequestId {get;set; }
publicboolShowRequestId=>!string.IsNullOrEmpty(RequestId);
publicstringExceptionMessage {get;set; }
privatereadonlyILogger_logger;
publicErrorModel(ILoggerlogger)
{
_logger=logger;
}
publicvoidOnGet()
{
RequestId=Activity.Current?.Id??HttpContext.TraceIdentifier;
varexceptionHandlerPathFeature=
HttpContext.Features.Get();
if(exceptionHandlerPathFeature?.ErrorisFileNotFoundException)
{
ExceptionMessage="File error thrown";
_logger.LogError(ExceptionMessage);
}
if(exceptionHandlerPathFeature?.Path=="/index")
{
ExceptionMessage+=" from home page";
}
}
}
Exception Handler lambda
除了使用Exception Handler Page之外,还可以使用lambda表达式来处理这个。
例如:
publicvoidConfigure(IApplicationBuilderapp,IWebHostEnvironmentenv)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(errorApp=>
{
errorApp.Run(asynccontext=>
{
context.Response.StatusCode=(int) HttpStatusCode.InternalServerError;;
context.Response.ContentType="text/html";
awaitcontext.Response.WriteAsync("\r\n");
awaitcontext.Response.WriteAsync("ERROR!\r\n");
varexceptionHandlerPathFeature=
context.Features.Get();
if(exceptionHandlerPathFeature?.ErrorisFileNotFoundException)
{
awaitcontext.Response.WriteAsync(
"File error thrown!\r\n");
}
awaitcontext.Response.WriteAsync(
"Home\r\n");
awaitcontext.Response.WriteAsync("\r\n");
awaitcontext.Response.WriteAsync(newstring(' ',512));
});
});
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints=>
{
endpoints.MapRazorPages();
});
}
Database error page
可以引入服务用来监视数据操作的错误。
publicvoidConfigureServices(IServiceCollectionservices)
{
services.AddDbContext(options=>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDatabaseDeveloperPageExceptionFilter();
services.AddDefaultIdentity(options=>options.SignIn.RequireConfirmedAccount=true)
.AddEntityFrameworkStores();
services.AddRazorPages();
}
另外还有和 不过这些都会放在后面的高级分来学些。我们这个分还是以基础为主。
往期文章目录:
ASP.net Core基础
AzureDeveloper,一个分享和学Azure技术的好去处,欢迎关注
免责声明:文中图片均来源于网络,如有版权问题请联系我们进行删除!
标签: