ASP.NET Core中实现全局异常拦截的完整步骤

2025-05-29 0 52

前言

异常是一种运行时错误,当异常没有得到适当的处理,很可能会导致你的程序意外终止,这篇就来讨论一下如何在 ASP.Net Core MVC 中实现全局异常处理,我会用一些 样例代码 和 截图 来说明这些概念。

全局异常处理

其实在 ASP.Net Core MVC 框架中已经有了全局异常处理的机制,你可以在一个中心化的地方使用 全局异常处理中间件 来进行异常拦截,如果不用这种中心化方式的话,你就只能在 Controller 或者 Action 作用域上单独处理,这会导致异常处理代码零散在项目各处,不好维护也特别麻烦,不是吗?

第二种处理 全局异常 的做法就是使用 exception filter,在本篇中,我准备跟大家聊一聊 全局异常处理中间件 和 UseExceptionHandler 方法来管控异常

使用 UseExceptionHandler 扩展方法

UseExceptionHandler 扩展方法能够将 ExceptionHandler 中间件注册到 Asp.net Core 的 请求处理管道 中,然后在 IExceptionHandlerFeature 接口的实例中获取 异常对象,下面的代码片段展示了如何使用 UseExceptionHandler 方法来截获全局异常

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37
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 类的定义。

?

1

2

3

4

5
public class ErrorMessage

{

public string Message { get; set; }

public string Stacktrace { get; set; }

}

配置 全局异常中间件

大家都知道,ASP.Net Core MVC 项目中都会有一个 Startup.cs 文件,可以在 Configure 方法下配置 全局异常拦截中间件 代码,如下所示:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
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 实现了 pipeline 注册,一旦应用程序出现了未处理异常,那么会自动将 用户 导向 /Error 页面。

你可以用 UseStatusCodePagesWithReExecute 扩展方法给 pipeline 添加一些状态码页面,这是什么意思呢? 其实也就是 http 500 导向 500 页面, http 404 导向 404 页面,下面的代码片段展示了修改后的 Configure 方法代码。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
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 下有一个专门处理错误的 action 方法,这里我们不使用这个 action,你可以把它删掉,接下来我准备定义一个专门的 ErrorController,里面包含了一个路由为 /Error 的 action 方法。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
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 来获取异常相关信息,也可以用 IStatusCodeReExecuteFeature 来获取 http 404 异常时当时的请求路径,对了,要想用上 IExceptionHandlerPathFeature 和 IStatusCodeReExecuteFeature ,要记得在 nuget 上安装了 Microsoft.AspNetCore.Diagnostics 包,下面的代码展示了如何获取异常发生时刻的路由地址。

?

1
string route = iExceptionHandlerFeature.Path;

如果想获取异常的详细信息,可以使用如下语句。

?

1
var exception = HttpContext.Features.Get<IExceptionHandlerPathFeature>();

一旦获取了这个路由地址和异常的详细信息,就可以将它记录到你的日志文件中,可供后续仔细分析。

使用 View 展示错误信息

可以创建一个 View 来展示出现的错误信息,下面时 Error ViewPage 的详细代码。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
@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 页面的 代码

?

1

2

3

4

5

6

7

8
@model string

@{

ViewData["Title"] = "NotFound";

Layout = "~/Views/Shared/_Layout.cshtml";

}

<h1 class="text-danger">

Error: The requested URL @Model was not found!</h1>

<hr />

现在可以把程序跑起来了,你会看到如下的错误信息。

ASP.NET Core中实现全局异常拦截的完整步骤

如果你尝试打开一个不存在的页面, 会自动跳转到 ErrorController.NotFound 所包装的 404 描述信息。

ASP.NET Core中实现全局异常拦截的完整步骤

ASP.NET Core 中内置了 全局异常处理,你可以利用这项技术在一个集中化的地方去截获你应用程序中的所有异常信息,当然你也可以基于环境的不同采取不用的异常处理措施,比如说:开发环境,测试环境,生产环境 等等。

译文链接: https://www.infoworld.com/art…

更多高质量干货:参见我的 GitHub:dotnetfly

到此这篇关于ASP.NET Core中实现全局异常拦截的文章就介绍到这了,更多相关ASP.NET Core全局异常拦截内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://segmentfault.com/a/1190000038770940

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 ASP.NET Core中实现全局异常拦截的完整步骤 https://www.kuaiidc.com/97366.html

相关文章

发表评论
暂无评论