這篇文章給大家分享的是有關(guān)ASP.NET Core異常和錯誤處理的案例的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考。一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)公司專注于企業(yè)成都全網(wǎng)營銷推廣、網(wǎng)站重做改版、回民網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、電子商務(wù)商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為回民等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。在這一章,我們將討論異常和錯誤處理。當 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ā)人員將非常有用。事實上,我們可能只希望當開發(fā)人員運行應(yīng)用程序時才顯示這些異常信息。
感謝各位的閱讀!關(guān)于ASP.NET Core異常和錯誤處理的案例就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
當前題目:ASP.NETCore異常和錯誤處理的案例-創(chuàng)新互聯(lián)
鏈接地址:http://www.chinadenli.net/article10/ccsego.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、做網(wǎng)站、定制開發(fā)、App設(shè)計、外貿(mào)建站、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容