做网站送给女友意义,wordpress 在线教育主题,做公司网站哪个好,网站点击按钮排序一、使用场景 在传统的ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那么默认的,当这个Action抛出了异常时MVC将会显示Error视图,该视图位于~/Views/Shared目录下。 自定义错误页面的…一、使用场景 在传统的ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那么默认的,当这个Action抛出了异常时MVC将会显示Error视图,该视图位于~/Views/Shared目录下。 自定义错误页面的目的就是为了能让程序在出现错误/异常的时候能够有较好的显示体验。有时候在Error视图中也会发生错误,这时ASP.NET/MVC将会显示其默认的错误页面(黄底红字),为了避免这种情况的出现,我们都是在Web.config文件的customErrors节中来自定义错误页面,来启用自定义错误处理 configurationsystem.webcompilation debugtrue /customErrors modeOn defaultRedirectDefaultErrorerror statusCode401 redirectHttp401Error/error statusCode403 redirectHttp403Error/error statusCode404 redirectHttp404Error/error statusCode500 redirectHttp500Error//customErrors/system.web
/configuration 二、.NET Core实现 既然想用ASP.NET Core中的中间件模拟Custom Error Page功能那首先我从配置下手。大家都知道.NET Core中配置文件系统发生了很大的变化默认都是采用Json格式的文件进行存储的当然配置文件也可以是其它类型的这里我们就不深入探讨了我们就围绕Json配置文件实现好了 ErrorPages: {401: /Error/Http401Page,403: /Error/Http403Page,404: /Error/Http404Page,500: /Error/Http500Page
} 我们在Startup类中定义两个变量用来存储配置文件读取出来的信息如下 public IConfigurationRoot Configuration { get; }internal static IDictionaryint, string ErrorPages { get; } new Dictionaryint, string(); 配置文件中定义的ErrorPages节点用于存储我们需要的Http状态编码并包含使用到的错误页面地址, 将他们用Startup类中的ErrorPages变量使用Key/Value的形式读取出来。 接下来我们要从JSON配置文件中读取信息填充到ErrorPages var builder new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile(appsettings.json, optional: true, reloadOnChange: true).AddJsonFile($appsettings.{env.EnvironmentName}.json, optional: true).AddEnvironmentVariables();Configuration builder.Build();foreach (var c in Configuration.GetSection(ErrorPages).GetChildren())
{var key Convert.ToInt32(c.Key);if (!ErrorPages.Keys.Contains(key)){ErrorPages.Add(key, c.Value);}
} 现在我们使用今天的主角创建一个ASP.NET Core的Middleware用于实现Custom Error Page功能 public class CustomErrorPagesMiddleware
{private readonly RequestDelegate _next;private readonly ILogger _logger;public CustomErrorPagesMiddleware(ILoggerFactory loggerFactory, RequestDelegate next){_next next;_logger loggerFactory.CreateLoggerCustomErrorPagesMiddleware();}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 (Startup.ErrorPages.Keys.Contains(statusCode)){context.Request.Path Startup.ErrorPages[statusCode];await _next(context);}}} 这样就完成了从响应Response的StatusCode到配置的具体页面的跳转。 当然我们最后还要为这个中间件添加一个扩展方法ASP.NET Core中为 IApplictionBuilder创建了好多的扩展方法其实也好比它的名子一样它就应该是一个建造者模式。 扩展方法如下 public static class BuilderExtensions
{public static IApplicationBuilder UseCustomErrorPages(this IApplicationBuilder app){return app.UseMiddlewareCustomErrorPagesMiddleware();}
} 最后在Startup类中的Configure方法中加入自定义错误的扩展 app.UseCustomErrorPages(); 三、源代码 如果你对文中的代码感兴趣也可以到我的Github上去看下这个例子的源代码https://github.com/maxzhang1985/CustomErrorPages ------------------分割线-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 开源推广 YOYOFx一个轻量级用于构建基于 HTTP 的 Web 服务支持.NET Framework 、.NET CORE、 Mono 平台。 本着学习的态度造了这个轮子也是为了更好的了解各个框架的原理和有点还希望可以和大家多交流 。 GitHubhttps://github.com/maxzhang1985/YOYOFx Star下 欢迎一起交流。 .NET Core 和 YOYOFx 的交流群 214741894 如果你觉得本文对你有帮助请点击“推荐”谢谢。 转载于:https://www.cnblogs.com/maxzhang1985/p/5974429.html