winform+dev的前后台分离标准项目
lg
2024-09-02 59abbe4785268a10ec9390b8373cce3939c1d24b
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Gs.Toolbox;
 
namespace Gs.Toolbox
{
    public class CustomApplicationModelConvention : IApplicationModelConvention
    {
        public void Apply(ApplicationModel application)
        {
            foreach (var controller in application.Controllers)
            {
                if (controller.ControllerType.IsAssignableTo(typeof(IRomteService)))
                {
                    ConfigureApiExplorer(controller);
                    ConfigureSelector(controller);
                    ConfigureParameters(controller);
                }
            }
        }
        //直接抄abp代码
        private void ConfigureParameters(ControllerModel controller)
        {
            foreach (var action in controller.Actions)
            {
                foreach (var prm in action.Parameters)
                {
                    if (prm.BindingInfo != null)
                    {
                        continue;
                    }
 
                    if (!TypeHelper.IsPrimitiveExtended(prm.ParameterInfo.ParameterType, includeEnums: true))
                    {
                        if (CanUseFormBodyBinding(action, prm))
                        {
                            prm.BindingInfo = BindingInfo.GetBindingInfo(new[] { new FromBodyAttribute() });
                        }
                    }
                }
            }
        }
        private bool CanUseFormBodyBinding(ActionModel action, ParameterModel parameter)
        {
            if (parameter.ParameterName == "id")
            {
                return false;
            }
 
            foreach (var selector in action.Selectors)
            {
                if (selector.ActionConstraints == null)
                {
                    continue;
                }
 
                foreach (var actionConstraint in selector.ActionConstraints)
                {
                    var httpMethodActionConstraint = actionConstraint as HttpMethodActionConstraint;
                    if (httpMethodActionConstraint == null)
                    {
                        continue;
                    }
 
                    if (httpMethodActionConstraint.HttpMethods.All(hm => hm.IsIn("GET", "DELETE", "TRACE", "HEAD")))
                    {
                        return false;
                    }
                }
            }
            return true;
        }
 
        private void ConfigureSelector(ControllerModel controller)
        {
            //先不使用特性,做个最简单的
            var controllerName = controller.ControllerName;
            //实际上路由凭借还是自己填补RouteAttribute特性
            foreach (var action in controller.Actions)
            {
                action.Selectors.Clear();
                var reqMethod = GetRequestMethod(action);
                action.Selectors.Add(new SelectorModel
                {
                    AttributeRouteModel = new AttributeRouteModel(new RouteAttribute($"/{controllerName}/{action.ActionName}")),
                    ActionConstraints = { new HttpMethodActionConstraint(new[] { reqMethod }) }
                });
            }
        }
 
        private string GetRequestMethod(ActionModel action)
        {
            var reqM = action.Attributes.OfType<RequestMethodAttribute>().ToList();
            return reqM.Last()._method.ToString();
        }
 
        private void ConfigureApiExplorer(ControllerModel controller)
        {
            //将自动发现的控制器显示出来
            controller.ApiExplorer.IsVisible = true;
            foreach (var action in controller.Actions)
            {
                ConfigureApiExplorer(action);
            }
        }
 
        private void ConfigureApiExplorer(ActionModel action)
        {
            action.ApiExplorer.IsVisible = true;
        }
    }
}