在ASP.NET Core使用Middleware模拟Custom Error Page功能
一、使用場景
在傳統的ASP.NET MVC中,我們可以使用HandleErrorAttribute特性來具體指定如何處理Action拋出的異常.只要某個Action設置了HandleErrorAttribute特性,那么默認的,當這個Action拋出了異常時MVC將會顯示Error視圖,該視圖位于~/Views/Shared目錄下。
自定義錯誤頁面的目的,就是為了能讓程序在出現錯誤/異常的時候,能夠有較好的顯示體驗。有時候在Error視圖中也會發生錯誤,這時ASP.NET/MVC將會顯示其默認的錯誤頁面(黃底紅字),為了避免這種情況的出現,我們都是在Web.config文件的customErrors節中來自定義錯誤頁面,來啟用自定義錯誤處理:
?
<configuration><system.web><compilation debug="true" /><customErrors mode="On" defaultRedirect="DefaultError"><error statusCode="401" redirect="Http401Error"/><error statusCode="403" redirect="Http403Error"/><error statusCode="404" redirect="Http404Error"/><error statusCode="500" redirect="Http500Error"/></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 IDictionary<int, string> ErrorPages { get; } = new Dictionary<int, 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.CreateLogger<CustomErrorPagesMiddleware>();}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.UseMiddleware<CustomErrorPagesMiddleware>();} }?
最后在Startup類中的Configure方法中加入自定義錯誤的擴展:
app.UseCustomErrorPages();三、源代碼
如果你對文中的代碼感興趣,也可以到我的Github上去看下這個例子的源代碼:https://github.com/maxzhang1985/CustomErrorPages
?
------------------分割線--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
開源推廣:
YOYOFx,一個輕量級用于構建基于 HTTP 的 Web 服務,支持.NET Framework 、.NET ?CORE、 Mono 平臺。
本著學習的態度,造了這個輪子,也是為了更好的了解各個框架的原理和有點,還希望可以和大家多交流 。
GitHub:https://github.com/maxzhang1985/YOYOFx? Star下, 歡迎一起交流。?.NET Core 和 YOYOFx 的交流群:?214741894??
如果你覺得本文對你有幫助,請點擊“推薦”,謝謝。
?
轉載于:https://www.cnblogs.com/maxzhang1985/p/5974429.html
總結
以上是生活随笔為你收集整理的在ASP.NET Core使用Middleware模拟Custom Error Page功能的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue 基于网易云API的短信验证码登录
- 下一篇: CruiseControl.NET与TF