Merge branch 'master_add_auth_1027' of http://192.168.5.200:8081/ql/apismnagaer_backend into master_add_auth_1027
This commit is contained in:
commit
3072fa9e88
@ -3,9 +3,11 @@ using Apimanager_backend.Exceptions;
|
||||
using Apimanager_backend.Services;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Apimanager_backend.Filters;
|
||||
|
||||
namespace Apimanager_backend.Controllers
|
||||
{
|
||||
[ModelValidationFilter()]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class UserController : ControllerBase
|
||||
|
||||
@ -4,8 +4,10 @@ namespace Apimanager_backend.Dtos
|
||||
{
|
||||
public class UserLoginDto
|
||||
{
|
||||
[MaxLength(20)]
|
||||
[MaxLength(20,ErrorMessage = "The maximum username is 20 characters")]
|
||||
public string UserName { get; set; }
|
||||
[MaxLength(50,ErrorMessage = "The maximum password is 50 characters")]
|
||||
[Required(ErrorMessage = "password is required")]
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
34
Apimanager_backend/Filters/ModelValidationFilter.cs
Normal file
34
Apimanager_backend/Filters/ModelValidationFilter.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Apimanager_backend.Dtos;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace Apimanager_backend.Filters
|
||||
{
|
||||
public class ModelValidationFilter : Attribute,IActionFilter
|
||||
{
|
||||
public void OnActionExecuted(ActionExecutedContext context) { }
|
||||
|
||||
public void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
if (!context.ModelState.IsValid)
|
||||
{
|
||||
// 将所有的错误信息收集到列表中
|
||||
var errors = context.ModelState.Values
|
||||
.SelectMany(v => v.Errors)
|
||||
.Select(e => e.ErrorMessage)
|
||||
.ToList();
|
||||
|
||||
// 构造统一的响应格式
|
||||
var response = new ResponseBase<object?>
|
||||
{
|
||||
Code = 1001, // 统一的错误码
|
||||
Message = errors[0],
|
||||
Data = null
|
||||
};
|
||||
|
||||
// 设置错误响应和 HTTP 状态码
|
||||
context.Result = new BadRequestObjectResult(response);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
using Apimanager_backend.Config;
|
||||
using Apimanager_backend.Data;
|
||||
using Apimanager_backend.Filters;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
@ -17,7 +18,10 @@ builder.Services.AddDbContext<ApiContext>(option =>
|
||||
option.UseMySql(constr, MySqlServerVersion.AutoDetect(constr))
|
||||
);
|
||||
builder.Services.AddAllService(configuration);
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddControllers(options =>
|
||||
{
|
||||
options.Filters.Add<ModelValidationFilter>();
|
||||
});
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user