异常过滤
Middleware过滤异常
API\Middlewares\ExceptionMiddleware.cs
| using System.Net; |
| using API.Contracts; |
| |
| namespace API.Middlewares; |
| |
| public class ExceptionMiddleware |
| { |
| private readonly RequestDelegate _next; |
| |
| public ExceptionMiddleware(RequestDelegate next) |
| { |
| _next = next; |
| } |
| |
| public async Task InvokeAsync(HttpContext context) |
| { |
| try |
| { |
| await _next(context); |
| } |
| catch (Exception ex) |
| { |
| context.Response.ContentType = "application/json"; |
| context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; |
| var error = new Error |
| { |
| StatusCode = context.Response.StatusCode.ToString(), |
| Message = $"Exception Middleware: {ex.Message}", |
| }; |
| await context.Response.WriteAsync(error.ToString()); |
| } |
| } |
| } |
API\Extensions\ApplicationBuilderExtensions.cs
| using API.Middlewares; |
| using Microsoft.AspNetCore.Builder; |
| |
| namespace API.Extensions; |
| |
| public static class ApplicationBuilderExtensions |
| { |
| public static IApplicationBuilder UseExceptionMiddleware(this IApplicationBuilder app) |
| { |
| return app.UseMiddleware<ExceptionMiddleware>(); |
| } |
| } |
| |
| |
API\Program.cs
| var app = builder.Build(); |
| |
| app.UseExceptionMiddleware(); |
| |
| app.Run(); |
Filter过滤异常
API\Filters\ExceptionFilter.cs
| using API.Contracts; |
| using Microsoft.AspNetCore.Mvc; |
| using Microsoft.AspNetCore.Mvc.Filters; |
| using System.Net; |
| |
| namespace API.Filters; |
| |
| public class ExceptionFilter : IExceptionFilter |
| { |
| public void OnException(ExceptionContext context) |
| { |
| |
| var error = new Error |
| { |
| StatusCode = HttpStatusCode.InternalServerError.ToString(), |
| Message = $"Exception Filter: {context.Exception.Message}", |
| }; |
| |
| context.Result = new JsonResult(error) |
| { |
| StatusCode = (int) HttpStatusCode.InternalServerError |
| }; |
| } |
| } |
| |
API\Program.cs
| var builder = WebApplication.CreateBuilder(args); |
| |
| |
| |
| builder.Services.AddControllers(config => |
| { |
| |
| config.Filters.Add(typeof(ExceptionFilter)); |
| }); |
| app.Run(); |
other
API\Contracts\Error.cs
| using System.Text.Json; |
| |
| namespace API.Contracts; |
| |
| public class Error |
| { |
| public string? StatusCode { get; set; } |
| public string? Message { get; set; } |
| |
| public override string ToString() |
| { |
| return JsonSerializer.Serialize(this); |
| } |
| } |
| |
API\Program.cs
| using API.Extensions; |
| using API.Filters; |
| |
| namespace API |
| { |
| public class Program |
| { |
| public static void Main(string[] args) |
| { |
| var builder = WebApplication.CreateBuilder(args); |
| |
| |
| builder.Services.AddControllers(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| builder.Services.AddEndpointsApiExplorer(); |
| builder.Services.AddSwaggerGen(); |
| |
| var app = builder.Build(); |
| |
| app.UseExceptionMiddleware(); |
| |
| if (app.Environment.IsDevelopment()) |
| { |
| app.UseSwagger(); |
| app.UseSwaggerUI(); |
| } |
| |
| app.UseHttpsRedirection(); |
| |
| app.UseAuthorization(); |
| |
| app.MapControllers(); |
| |
| app.Run(); |
| } |
| } |
| } |
| |
| |
| |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2023-05-22 webgpu_红色三角形_学习_wgsl