hutongqing
2024-12-10 8d341db9d2d5699d527c88c935f0c4ce255a57a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Attributes;
 
namespace WIDESEA_Core.Utilities
{
    public static class ParamsValidator
    {
        public static void ActionParamsValidator(this ActionExecutingContext context)
        {
            List<object> obj = context.ActionDescriptor.EndpointMetadata.ToList();
 
            if (obj.Any(x => x is MethodParamsValidateAttribute))
            {
                foreach (var param in context.ActionDescriptor.Parameters)
                {
                    ModelValidateAttribute? modelValidate = param.ParameterType.GetCustomAttribute<ModelValidateAttribute>();
                    if (modelValidate != null && context.ActionArguments.TryGetValue(param.Name, out object? value) && value != null)
                    {
                        (bool, string, object?) result = ModelValidate.ValidateModelData(value, param.ParameterType);
                        if (!result.Item1)
                        {
                            context.Result = new JsonResult(new WebResponseContent()
                            {
                                Status = false,
                                Data = result.Item3,
                                Message = result.Item2
                            });
                        }
                    }
                    //else
                    //{
                    //    if (param.ParameterType == typeof(SaveModel) && context.ActionArguments.TryGetValue(param.Name, out value) && value != null)
                    //    {
 
                    //    }
                    //}
                }
            }
        }
    }
}