Student.Core.API/Code/Middleware/ExceptionHandleMiddleware.cs
(完整代码) 1KBusing System;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using yrjw.CommonToolsCore.Helper;
using yrjw.ORM.Chimp.Result;
namespace Student.Core.API.Code.Middleware
{
public class ExceptionHandleMiddleware
{
private readonly RequestDelegate _next;
private readonly IHostEnvironment _env;
private readonly ILogger _logger;
public ExceptionHandleMiddleware(RequestDelegate next, IHostEnvironment env, ILogger<ExceptionHandleMiddleware> logger)
{
_next = next;
_env = env;
_logger = logger;
}
public async Task InvokeAsync(HttpContext httpContext)
{
try
{
await _next(httpContext);
}
catch (Exception ex)
{
await HandleExceptionAsync(httpContext, ex);
}
}
private Task HandleExceptionAsync(HttpContext context, Exception exception)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var error = _env.IsDevelopment() ? exception.ToString() : exception.Message;
_logger.LogError(error);
return context.Response.WriteAsync(JsonHelper.SerializeJSON(ResultModel.Failed(error, 500)));
}
}
}
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。