ASP.NET Core異常和錯誤處理(8)
在這一章,我們將討論異常和錯誤處理。當(dāng) ASP.NET Core應(yīng)用程序中發(fā)生錯誤時,您可以以各種不同的方式來處理。讓我們來看看通過添加一個中間件來處理異常情況,這個中間件將幫助我們處理錯誤。
要模擬出錯,讓我們轉(zhuǎn)到應(yīng)用程序,運行,如果我們只是拋出異常的話,看看程序是如何運轉(zhuǎn)轉(zhuǎn)的。
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
namespace FirstAppDemo {
public class Startup {
public Startup() {
var builder = new ConfigurationBuilder()
.AddJsonFile("AppSettings.json");
Configuration = builder.Build();
}
public IConfiguration Configuration { get; set; }
// This method gets called by the runtime.
// Use this method to add services to the container.
// For more information on how to configure your application,
// visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) {
}
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app) {
app.UseIISPlatformHandler();
app.UseRuntimeInfoPage();
app.Run(async (context) => {
throw new System.Exception("Throw Exception");
var msg = Configuration["message"];
await context.Response.WriteAsync(msg);
});
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}
它只會拋出一個非常通用的異常信息。保存Startup.cs頁面并運行您的應(yīng)用程序。

您將看到我們未能加載此資源。出現(xiàn)了一個 HTTP 500 錯誤,內(nèi)部服務(wù)器錯誤,那個頁面不是很有幫助。它可能很方便得到一些異常信息。
讓我們添加另一個中間件 UseDeveloperExceptionPage。
// This method gets called by the runtime.
// Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app) {
app.UseIISPlatformHandler();
app.UseDeveloperExceptionPage();
app.UseRuntimeInfoPage();
app.Run(async (context) => {
throw new System.Exception("Throw Exception");
var msg = Configuration["message"];
await context.Response.WriteAsync(msg);
});
}
這個中間件與其他的有點不同,其他中間件通常監(jiān)聽傳入的請求并對請求做一些響應(yīng)。
UseDeveloperExceptionPage不會如此在意傳入的請求在之后的管道會發(fā)生什么。
它只是調(diào)用下一個中間件,然后再等待,看看管道中是否會出現(xiàn)異常,如果有異常,這塊中間件會給你一個關(guān)于該異常的錯誤頁面。
現(xiàn)在讓我們再次運行應(yīng)用程序。將會產(chǎn)生一個如下面的屏幕截圖所示的輸出。

現(xiàn)在如果程序中出現(xiàn)異常,您將在頁面中看到一些想要看到的異常信息。你也會得到一個堆棧跟蹤:這里可以看到Startup.cs第37行有一個未處理的異常拋出。
所有這些異常信息對開發(fā)人員將非常有用。事實上,我們可能只希望當(dāng)開發(fā)人員運行應(yīng)用程序時才顯示這些異常信息。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- .NET?8新預(yù)覽版使用?Blazor?組件進(jìn)行服務(wù)器端呈現(xiàn)(項目體驗)
- 國產(chǎn)化中的?.NET?Core?操作達(dá)夢數(shù)據(jù)庫DM8的兩種方式(操作詳解)
- 解決Win10無法安裝.Net Framework 3.5提示錯誤代碼0x800F081F
- win8/8.1系統(tǒng)安裝.net framework 3.5出現(xiàn)0x800F0906代碼錯誤的解決方法
- ASP.NET MVC5+EF6+EasyUI 后臺管理系統(tǒng)(81)-數(shù)據(jù)篩選(萬能查詢)實例
- ASP.NET 程序員都非常有用的85個工具
- 無法啟動.NET Framework NGEN v4.0.30319_X86服務(wù)的解決方法
- .NET8 依賴注入
相關(guān)文章
ASP.NET MVC 3仿Server.Transfer效果的實現(xiàn)方法
這篇文章主要介紹了ASP.NET MVC 3仿Server.Transfer效果的實現(xiàn)方法,需要的朋友可以參考下2015-10-10
asp.net實現(xiàn)DropDownList,TreeView,ListBox的無限極分類目錄樹
這篇文章主要介紹了asp.net實現(xiàn)DropDownList,TreeView,ListBox的無限極分類目錄樹,結(jié)合實例形式較為詳細(xì)的分析了asp.net常見控件實現(xiàn)無限極分類目錄樹的具體實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-06-06
ASP.NET Core MVC獲取請求的參數(shù)方法示例
這篇文章主要給大家介紹了關(guān)于ASP.NET Core MVC是如何獲取請求的參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用ASP.NET Core MVC具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05

