(7)处理 ASP.NET Core 中的错误 1. 前言
ASP.NET Core 处理错误环境区分为两种: 开发环境和非开发环境.
开发环境: 开发人员异常页.
非开发环境: 异常处理程序页, 状态代码页.
- public void Configure(IApplicationBuilder App, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- // 开发环境
- }
- else
- {
- // 非开发环境
- }
- }
env.IsDevelopment()是判断应用程序运行是在开发环境还是非开发环境, 具体配置在 Properties/launchSettings.JSON, 找到 ASPNETCORE_ENVIRONMENT 属性, 默认值是开发环境(Development), 具体环境配置知识点后面我们再来学习下.
2. 开发人员异常页
向 Startup.Configure 方法添加代码, 以当应用在开发环境中运行时启用此页:
- if (env.IsDevelopment())
- {
- App.UseDeveloperExceptionPage();
- }
开发人员异常页仅当应用程序在开发环境中运行时才会启用, 而且调用 UseDeveloperExceptionPage 要配置于任何要捕获其异常的中间件前面.
3. 异常处理程序页
在下面的示例中, UseExceptionHandler 在非开发环境中添加异常处理中间件:
- if (env.IsDevelopment())
- {
- App.UseDeveloperExceptionPage();
- }
- else
- {
- App.UseExceptionHandler("/Error");
- App.UseHsts();
- }
Razor Pages 应用模板提供 "页面" 文件夹中的 Error 页 (.cshtml) 和 PageModel 类(ErrorModel). 对于 MVC 应用, 项目模板包括 Error 操作方法和 Error 视图. 操作方法如下:
- [AllowAnonymous]
- [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
- public IActionResult Error()
- {
- return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
- }
不要使用 HTTP 方法属性 (如 HttpGet) 修饰错误处理程序操作方法, 因为会阻止某些请求访问的方法. 同时最好允许匿名访问方法, 以便未经身份验证的用户能够接收错误视图.
- if (env.IsDevelopment())
- {
- App.UseDeveloperExceptionPage();
- }
- else
- {
- App.UseExceptionHandler(errorApp =>
- {
- errorApp.Run(async context =>
- {
- context.Response.StatusCode = 500;
- context.Response.ContentType = "text/html";
- await context.Response.WriteAsync("\r\n");
- await context.Response.WriteAsync("ERROR!
- \r\n");
- var exceptionHandlerPathFeature =
- context.Features.Get();
- // Use exceptionHandlerPathFeature to process the exception (for example,
- // logging), but do NOT expose sensitive error information directly to
- // the client.
- if (exceptionHandlerPathFeature?.Error is FileNotFoundException)
- {
- await context.Response.WriteAsync("File error thrown!
- \r\n");
- }
- await context.Response.WriteAsync("Home
- \r\n");
- await context.Response.WriteAsync("\r\n");
- await context.Response.WriteAsync(new string(' ', 512)); // IE padding
- });
- });
- App.UseHsts();
- }
4. 状态代码页
一般情况下, ASP.NET Core 应用程序不会为 HTTP 状态代码 (如 "404 - 未找到") 提供状态代码页的. 但若要提供状态代码页, 可以使用状态代码页中间件.
4.1 UseStatusCodePages 中间件
若要启用常见错误状态代码的默认纯文本处理程序, 请在 Startup.Configure 方法中调用 UseStatusCodePages:
App.UseStatusCodePages();
而这里有一点要注意的是, 调用 UseStatusCodePages 中间件要在例如静态文件中间件和 MVC 中间件等中间件前面调用:
- App.UseStatusCodePages();
- App.UseStaticFiles();
- App.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
下面通过运行应用程序在浏览器地址栏上输入一个不存在地址看看配置该中间件后的效果:
很显然当我们输入一个不存在地址之后就会打开一个处理错误的状态代码页.
App.UseStatusCodePages("text/plain", "Status code page, status code: {0}");
●包含 lambda 的 UseStatusCodePages:
- App.UseStatusCodePages(async context =>
- {
- context.HttpContext.Response.ContentType = "text/plain";
- await context.HttpContext.Response.WriteAsync(
- "Status code page, status code:" +
- context.HttpContext.Response.StatusCode);
- });
4.2 UseStatusCodePagesWithRedirect 中间件
●向客户端发送 "302 - 已找到" 状态代码.
App.UseStatusCodePagesWithRedirects("/Error/{0}");
运行应用程序在浏览器上输入不存在地址 https://localhost:44353/1 看看配置该中间件后的效果, 你会发觉当我们输入上述地址后会跳转到 https://localhost:44353/Error/404 链接去了, 并显示:
这就说明白当我们输入一个不存在地址之后会重定向中间件设置的地址页面去了.
参考文献:
来源: https://www.2cto.com/kf/201905/810509.html