ASP.NET Core中實(shí)現(xiàn)全局異常攔截的完整步驟
前言
異常是一種運(yùn)行時(shí)錯(cuò)誤,當(dāng)異常沒有得到適當(dāng)?shù)奶幚?,很可能會?dǎo)致你的程序意外終止,這篇就來討論一下如何在 ASP.Net Core MVC 中實(shí)現(xiàn)全局異常處理,我會用一些 樣例代碼 和 截圖 來說明這些概念。
全局異常處理
其實(shí)在 ASP.Net Core MVC 框架中已經(jīng)有了全局異常處理的機(jī)制,你可以在一個(gè)中心化的地方使用 全局異常處理中間件 來進(jìn)行異常攔截,如果不用這種中心化方式的話,你就只能在 Controller 或者 Action 作用域上單獨(dú)處理,這會導(dǎo)致異常處理代碼零散在項(xiàng)目各處,不好維護(hù)也特別麻煩,不是嗎?
第二種處理 全局異常 的做法就是使用 exception filter,在本篇中,我準(zhǔn)備跟大家聊一聊 全局異常處理中間件 和 UseExceptionHandler 方法來管控異常。
使用 UseExceptionHandler 擴(kuò)展方法
UseExceptionHandler 擴(kuò)展方法能夠?qū)?ExceptionHandler 中間件注冊到 Asp.net Core 的 請求處理管道 中,然后在 IExceptionHandlerFeature 接口的實(shí)例中獲取 異常對象,下面的代碼片段展示了如何使用 UseExceptionHandler 方法來截獲全局異常。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseExceptionHandler(builder =>
{
builder.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var exception = context.Features.Get<IExceptionHandlerFeature>();
if (exception != null)
{
var error = new ErrorMessage()
{
Stacktrace = exception.Error.StackTrace,
Message = exception.Error.Message
};
var errObj = JsonConvert.SerializeObject(error);
await context.Response.WriteAsync(errObj).ConfigureAwait(false);
}
});
}
);
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
下面是代碼中引用的 ErrorMessage 類的定義。
public class ErrorMessage
{
public string Message { get; set; }
public string Stacktrace { get; set; }
}
配置 全局異常中間件
大家都知道,ASP.Net Core MVC 項(xiàng)目中都會有一個(gè) Startup.cs 文件,可以在 Configure 方法下配置 全局異常攔截中間件 代碼,如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template:
"{controller=Home}/{action=Index}/{id?}");
});
}
可以著重看一下上面的 app.UseExceptionHandler("/Error"); ,這里的 UseExceptionHandler 實(shí)現(xiàn)了 pipeline 注冊,一旦應(yīng)用程序出現(xiàn)了未處理異常,那么會自動(dòng)將 用戶 導(dǎo)向 /Error 頁面。
你可以用 UseStatusCodePagesWithReExecute 擴(kuò)展方法給 pipeline 添加一些狀態(tài)碼頁面,這是什么意思呢? 其實(shí)也就是 http 500 導(dǎo)向 500 頁面, http 404 導(dǎo)向 404 頁面,下面的代碼片段展示了修改后的 Configure 方法代碼。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseStatusCodePagesWithReExecute("/Error/NotFound/{0}");
}
//Other code
}
使用 ErrorController
在 HomeController 下有一個(gè)專門處理錯(cuò)誤的 action 方法,這里我們不使用這個(gè) action,你可以把它刪掉,接下來我準(zhǔn)備定義一個(gè)專門的 ErrorController,里面包含了一個(gè)路由為 /Error 的 action 方法。
public class ErrorController : Controller
{
[HttpGet("/Error")]
public IActionResult Index()
{
IExceptionHandlerPathFeature iExceptionHandlerFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
if (iExceptionHandlerFeature != null)
{
string path = iExceptionHandlerFeature.Path;
Exception exception = iExceptionHandlerFeature.Error;
//Write code here to log the exception details
return View("Error",iExceptionHandlerFeature);
}
return View();
}
[HttpGet("/Error/NotFound/{statusCode}")]
public IActionResult NotFound(int statusCode)
{
var iStatusCodeReExecuteFeature =HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
return View("NotFound",iStatusCodeReExecuteFeature.OriginalPath);
}
}
你可以用 IExceptionHandlerPathFeature 來獲取異常相關(guān)信息,也可以用 IStatusCodeReExecuteFeature 來獲取 http 404 異常時(shí)當(dāng)時(shí)的請求路徑,對了,要想用上 IExceptionHandlerPathFeature 和 IStatusCodeReExecuteFeature ,要記得在 nuget 上安裝了 Microsoft.AspNetCore.Diagnostics 包,下面的代碼展示了如何獲取異常發(fā)生時(shí)刻的路由地址。
string route = iExceptionHandlerFeature.Path;
如果想獲取異常的詳細(xì)信息,可以使用如下語句。
var exception = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
一旦獲取了這個(gè)路由地址和異常的詳細(xì)信息,就可以將它記錄到你的日志文件中,可供后續(xù)仔細(xì)分析。
使用 View 展示錯(cuò)誤信息
可以創(chuàng)建一個(gè) View 來展示出現(xiàn)的錯(cuò)誤信息,下面時(shí) Error ViewPage 的詳細(xì)代碼。
@model Microsoft.AspNetCore.Diagnostics.IExceptionHandlerFeature
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="row">
<div class="text-danger">
<h3>Error: @Model.Error.Message</h3>
</div>
</div>
<div class="row">
<div class="col-12">
<p>@Model.Error.StackTrace</p>
<p>@Model.Error.InnerException</p>
</div>
</div>
下面是 NotFound 頁面的 代碼
@model string
@{
ViewData["Title"] = "NotFound";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1 class="text-danger">
Error: The requested URL @Model was not found!</h1>
<hr />
現(xiàn)在可以把程序跑起來了,你會看到如下的錯(cuò)誤信息。

如果你嘗試打開一個(gè)不存在的頁面, 會自動(dòng)跳轉(zhuǎn)到 ErrorController.NotFound 所包裝的 404 描述信息。

ASP.NET Core 中內(nèi)置了 全局異常處理,你可以利用這項(xiàng)技術(shù)在一個(gè)集中化的地方去截獲你應(yīng)用程序中的所有異常信息,當(dāng)然你也可以基于環(huán)境的不同采取不用的異常處理措施,比如說:開發(fā)環(huán)境,測試環(huán)境,生產(chǎn)環(huán)境 等等。
譯文鏈接: https://www.infoworld.com/art...
更多高質(zhì)量干貨:參見我的 GitHub:dotnetfly
到此這篇關(guān)于ASP.NET Core中實(shí)現(xiàn)全局異常攔截的文章就介紹到這了,更多相關(guān)ASP.NET Core全局異常攔截內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ASP.NET實(shí)現(xiàn)按拼音碼模糊查詢的方法
我們在做數(shù)據(jù)錄入或者查詢的時(shí)候,經(jīng)常需要實(shí)現(xiàn)按用戶輸入的拼音碼進(jìn)行數(shù)據(jù)的模糊查詢功能,本文為大家介紹ASP.NET如何實(shí)現(xiàn)按拼音碼模糊查詢,需要的朋友可以參考下2015-09-09
完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁
這篇文章主要介紹了完美兼容ie和firefox的asp.net網(wǎng)站加入收藏和設(shè)置主頁,需要的朋友可以參考下2014-12-12
C#調(diào)用C++版本dll時(shí)的類型轉(zhuǎn)換需要注意的問題小結(jié)
最近使用C#調(diào)用C++版本的dll遇到很多類型轉(zhuǎn)換的問題,現(xiàn)記錄出容易出錯(cuò)的部分。2010-04-04
GridView控件實(shí)現(xiàn)數(shù)據(jù)的顯示和刪除(第8節(jié))
這篇文章主要介紹了GridView控件實(shí)現(xiàn)數(shù)據(jù)的顯示和刪除,以新聞網(wǎng)站為例,實(shí)現(xiàn)對新聞數(shù)據(jù)的操作,了解各種數(shù)據(jù)源控件與數(shù)據(jù)綁定控件的類型和作用,需要的朋友可以參考下2015-08-08
如何在ASP.NET Core中給上傳圖片功能添加水印實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于如何在ASP.NET Core中給上傳圖片功能添加水印的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
asp.net gridview中用checkbox全選的幾種實(shí)現(xiàn)的區(qū)別
這幾天為了改變客戶端grid的全選效率問題,詳細(xì)研究了ext中g(shù)rid的全選和gridview中通過腳本實(shí)現(xiàn)的全選效率,總結(jié)一下,供大家參考,有錯(cuò)誤的地方,希望大俠指正,小弟獻(xiàn)丑了。2009-06-06

