当前位置:网站首页>. net core uses iasyncexceptionfilter to capture global exceptions and return information uniformly
. net core uses iasyncexceptionfilter to capture global exceptions and return information uniformly
2022-04-22 21:14:00 【Xiaodou whole sugar (Cx)】
Create a new one API project (.net 6)

add to ExceptionHandel class
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace ExceptionFilter
{
public class ExceptionHandel : IAsyncExceptionFilter
{
private readonly IWebHostEnvironment _webHostEnvironment;
public ExceptionHandel(IWebHostEnvironment webHostEnvironment)
{
_webHostEnvironment = webHostEnvironment;
}
public Task OnExceptionAsync(ExceptionContext context)
{
string msg = string.Empty;
if (_webHostEnvironment.IsDevelopment())
{
msg = context.Exception.ToString();
}
else
{
msg = $" Server internal exception {
context.ActionDescriptor.DisplayName}";
}
ObjectResult objectResult = new ObjectResult(new {
code = 500, msg = msg });
context.Result = objectResult;
context.ExceptionHandled = true;
return Task.CompletedTask;
}
}
}
Inject
builder.Services.Configure<MvcOptions>(opt =>
{
opt.Filters.Add<ExceptionHandel>();
});
Make an error report API
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ExceptionFilter.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public Task getValue()
{
return System.IO.File.ReadAllLinesAsync("1.txt");
}
}
}
版权声明
本文为[Xiaodou whole sugar (Cx)]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221816273502.html
边栏推荐
猜你喜欢

Selenium_ Webdriver video automation script sharing
![[server data recovery] a data recovery case in which multiple hard disks are disconnected and the server crashes due to water ingress in the server](/img/e9/5a94aef710f0185a432a459f67fa75.jpg)
[server data recovery] a data recovery case in which multiple hard disks are disconnected and the server crashes due to water ingress in the server

Big talk test data (II): acquisition of concept test data

Use of dSPACE simulation platform

Summary of software testing knowledge points | JMeter implementation Interface Association

Openvx's immediate mode and graph mode and examples

Lizard book learning Day1 - overview of machine learning

如何用laragon框架运行php文件

Soochow securities x kangaroo cloud: the data is easily available and has millisecond response ability. What did Soochow securities do right?

MySQL is still suitable for Silicon Valley courses (I)
随机推荐
BLE---Advertisement data format & service
Summary of software testing knowledge points | JMeter implementation Interface Association
【工具配置篇】Visual Studio 中由 designer 生成的. ui 文件未能及時更新問題的解决方案
cuda10.2安装torch1.9
简单介绍常用数据库引擎
Return to heaven ladder-l2-020 Kung Fu successor (25 points) (BFS or DFS)
Webmaster Tools - Webmaster software website - Webmaster Tools website - Webmaster essential tools free
[Istio是什么?] 还不知道你就out了,一文40分钟快速理解
News Express I mobtech passed the "special safety evaluation" of China Academy of information and communications
毕业五年,如何从月薪5K涨到年薪50W+,需要掌握哪些核心技能?
345-Leetcode 赎金信
新闻速递 I MobTech通过中国信通院“安全专项评测”
MySql指定字段排序、自定义排序位置
R language data reading, cleaning and univariate linear regression
MySQL尚硅谷课程查缺补漏适合基础(一)
Openvx - read and write image file [pgm format] as VX_ Image objects, and write operations
Basic principle and preliminary practice of building local server and XSS by wampserver (I)
. 102 keyboard move div
软件测试工具最全的抓包工具的综合对比
重返天梯-L2-020 功夫传人 (25 分)(bfs or dfs)