分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

学习Net Core 2.0 做博客 错误页中间件

发布时间:2023-09-06 01:21责任编辑:熊小新关键词:暂无标签

CustomErrorPagesMiddleware

public class CustomErrorPagesMiddleware ???{ ???????private readonly RequestDelegate _next; ???????private readonly Microsoft.Extensions.Logging.ILogger _logger; ???????private readonly IHostingEnvironment _env; ???????public CustomErrorPagesMiddleware( ???????????RequestDelegate next, ???????????ILoggerFactory loggerFactory, ???????????IHostingEnvironment env) ???????{ ???????????_next = next; ???????????_logger = loggerFactory.CreateLogger<CustomErrorPagesMiddleware>(); ???????????_env = env; ???????} ???????public async Task Invoke(HttpContext context) ???????{ ???????????try ???????????{ ???????????????await _next(context); ???????????} ???????????catch (Exception ex) ???????????{ ???????????????_logger.LogError(0, ex, "An unhandled exception has occurred while executing the request"); ???????????????if (context.Response.HasStarted) ???????????????{ ???????????????????_logger.LogWarning("The response has already started, the error page middleware will not be executed."); ???????????????????throw; ???????????????} ???????????????try ???????????????{ ???????????????????context.Response.Clear(); ???????????????????context.Response.StatusCode = 500; ???????????????????return; ???????????????} ???????????????catch (Exception ex2) ???????????????{ ???????????????????_logger.LogError(0, ex2, "An exception was thrown attempting to display the error page."); ???????????????} ???????????????throw; ???????????} ???????????finally ???????????{ ???????????????var statusCode = context.Response.StatusCode; ???????????????if (statusCode == 404 || statusCode == 500|| statusCode == 403) ???????????????{ ?????????????????????????????????????await ErrorPage.ResponseAsync(context.Response, statusCode,_env); ???????????????} ???????????????????????????} ???????} ???}

ErrorPage

 public static class ErrorPage ???{ ???????public static async Task ResponseAsync(HttpResponse response, int statusCode, IHostingEnvironment env) ???????{ ???????????if (statusCode == 404) ???????????{ ?????????????????????????????????await response.WriteAsync(Page404,Encoding.UTF8); ??????????????????????} ???????????else if (statusCode == 500) ???????????{ ???????????????await response.WriteAsync(Page500); ???????????}else if (statusCode == 403) ???????????{ ???????????  //没有权限服务器会返回403; ???????????????await response.WriteAsync(Page403); ???????????} ???????} ???????private static string Page404 => "404"; ???????private static string Page500 => "500"; ???????private static string Page403 => "403"; ???}

CustomErrorPagesExtensions

 public static class CustomErrorPagesExtensions ???{ ???????public static IApplicationBuilder UseCustomErrorPages(this IApplicationBuilder app) ???????{ ???????????if (app == null) ???????????{ ???????????????throw new ArgumentNullException(nameof(app)); ???????????} ???????????return app.UseMiddleware<CustomErrorPagesMiddleware>(); ???????} ???}

Startup

 app.UseCustomErrorPages();

学习Net Core 2.0 做博客 错误页中间件

原文地址:http://www.cnblogs.com/wyzy/p/7749908.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved