怪物奇妙物语

宇宙无敌超级美少男的怪物奇妙物语

首页 新随笔 联系 管理
  819 随笔 :: 0 文章 :: 2 评论 :: 16万 阅读

异常过滤

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();
// exception handling middleware
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);
// Add services to the container.
// builder.Services.AddControllers();
builder.Services.AddControllers(config =>
{
// global exception handling filter
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);
// Add services to the container.
builder.Services.AddControllers();
// builder.Services.AddControllers(config =>
// {
// // global exception handling filter
// config.Filters.Add(typeof(ExceptionFilter));
// });
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// exception handling middleware
app.UseExceptionMiddleware();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
}
}
posted on   超级无敌美少男战士  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
历史上的今天:
2023-05-22 webgpu_红色三角形_学习_wgsl
点击右上角即可分享
微信分享提示