新框架PC后端代码(祈禧6月初版本)
南骏 池
4 天以前 72449a1b8699b65712e57fba8abce5a8240e9465
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
using Gs.Toolbox.ApiCore.Abstract.Mvc;
using Gs.Toolbox.ApiCore.Common.Helper;
using Gs.Toolbox.ApiCore.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ActionConstraints;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.ModelBinding;
 
namespace Gs.Toolbox.ApiCore.Common.Mvc;
 
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);
            if (!string.IsNullOrEmpty(reqMethod))
                action.Selectors.Add(new SelectorModel
                {
                    AttributeRouteModel = new AttributeRouteModel(
                        new RouteAttribute(
                            $"/{controllerName}/{action.ActionName}")),
                    ActionConstraints =
                        { new HttpMethodActionConstraint(new[] { reqMethod }) }
                });
        }
    }
 
    private string GetRequestMethod(ActionModel action)
    {
        try
        {
            var reqM = action.Attributes.OfType<RequestMethodAttribute>()
                .ToList();
            return reqM.Last()._method.ToString();
        }
        catch (Exception ex)
        {
            return "";
        }
    }
 
    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;
    }
}