1. 新增并完善ResultCode枚举,补充标准HTTP状态码对应的业务状态码 2. 重构BusinessException,新增基于ResultCode的构造函数和ThrowIf扩展方法 3. 替换所有硬编码的HTTP状态码为统一的ResultCode枚举 4. 优化全局异常中间件,根据业务状态码映射对应HTTP状态码并规范化JSON响应 5. 修复OssImageHelper和AutoDotCodeConsumer中的OSS文件处理逻辑 6. 新增用户答题快照实体类 7. 清理废弃的宠物模块迁移脚本
46 lines
1.5 KiB
C#
46 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using QYZH.InteractiveMagazine.Models.Dto;
|
|
|
|
|
|
namespace QYZH.InteractiveMagazine.Infrastructure.Middleware;
|
|
|
|
/// <summary>
|
|
/// 数据验证过滤器
|
|
/// </summary>
|
|
public class ModelValidActionFilterAttribute : ActionFilterAttribute
|
|
{
|
|
public override void OnActionExecuting(ActionExecutingContext context)
|
|
{
|
|
if (!context.ModelState.IsValid)
|
|
{
|
|
Dictionary<string, string> errorDic = new Dictionary<string, string>();
|
|
foreach (var key in context.ModelState.Keys)
|
|
{
|
|
var modelstate = context.ModelState[key];
|
|
if (modelstate.Errors.Any())
|
|
{
|
|
string errorStr = string.Join(",", modelstate.Errors.Select(e => e.ErrorMessage).ToList());
|
|
errorDic.Add(key, errorStr);
|
|
}
|
|
}
|
|
var result = new BaseResponse<Dictionary<string, string>>() { code = ResultCode.BAD_REQUEST };
|
|
|
|
result.message = string.Join("|", errorDic.Select(e => e.Value).Distinct());
|
|
result.result = errorDic;
|
|
context.Result = new JsonResult(result);
|
|
}
|
|
}
|
|
|
|
//public override void OnActionExecuted(ActionExecutedContext context)
|
|
//{
|
|
// if (context.Exception != null)
|
|
// {
|
|
// BaseResponse result = new BaseResponse() { Code = ResultCode.Fail };
|
|
// result.Message = context.Exception.Message;
|
|
// context.Result = new JsonResult(result);
|
|
// }
|
|
//}
|
|
}
|
|
|