@@ -9,10 +9,17 @@ public abstract class ServiceBase : IService
9
9
10
10
public string BaseUri { get ; init ; }
11
11
12
- public ServiceRouteOptions Route { get ; } = new ( ) ;
12
+ public ServiceRouteOptions RouteOptions { get ; } = new ( ) ;
13
13
14
14
public string ? ServiceName { get ; init ; }
15
15
16
+ /// <summary>
17
+ /// Based on the RouteHandlerBuilder extension, it is used to extend the mapping method, such as
18
+ /// RouteHandlerBuilder = routeHandlerBuilder =>
19
+ /// {
20
+ /// routeHandlerBuilder.RequireAuthorization("AtLeast21");
21
+ /// };
22
+ /// </summary>
16
23
public Action < RouteHandlerBuilder > ? RouteHandlerBuilder { get ; init ; }
17
24
18
25
public IServiceCollection Services => MasaApp . Services ;
@@ -54,20 +61,24 @@ internal void AutoMapRoute(ServiceGlobalRouteOptions globalOptions, Pluralizatio
54
61
string ? pattern = null ;
55
62
string ? httpMethod = null ;
56
63
string ? methodName = null ;
57
- var atttibute = method . GetCustomAttribute < RoutePatternAttribute > ( ) ;
58
- if ( atttibute != null )
64
+ var attribute = method . GetCustomAttribute < RoutePatternAttribute > ( ) ;
65
+ if ( attribute != null )
59
66
{
60
- httpMethod = atttibute . HttpMethod ;
61
- if ( atttibute . StartWithBaseUri )
62
- methodName = atttibute . Pattern ;
67
+ httpMethod = attribute . HttpMethod ;
68
+ if ( attribute . StartWithBaseUri )
69
+ methodName = attribute . Pattern ;
63
70
else
64
- pattern = atttibute . Pattern ;
71
+ pattern = attribute . Pattern ;
65
72
}
66
73
67
74
string newMethodName = method . Name ;
68
75
69
76
if ( httpMethod == null || pattern == null )
70
- httpMethod ??= TryGetHttpMethod ( globalOptions , ref newMethodName ) ;
77
+ {
78
+ var result = ParseMethod ( globalOptions , newMethodName ) ;
79
+ httpMethod ??= result . HttpMethod ;
80
+ newMethodName = result . MethodName ;
81
+ }
71
82
72
83
pattern ??= ServiceBaseHelper . CombineUris ( GetBaseUri ( globalOptions , pluralizationService ) ,
73
84
methodName ?? GetMethodName ( method , newMethodName , globalOptions ) ) ;
@@ -91,9 +102,10 @@ protected virtual string GetBaseUri(ServiceRouteOptions globalOptions, Pluraliza
91
102
92
103
var list = new List < string > ( )
93
104
{
94
- Route . Prefix ?? globalOptions . Prefix ?? string . Empty ,
95
- Route . Version ?? globalOptions . Version ?? string . Empty ,
96
- ServiceName ?? GetServiceName ( Route . PluralizeServiceName ?? globalOptions . PluralizeServiceName ?? false ? pluralizationService :
105
+ RouteOptions . Prefix ?? globalOptions . Prefix ?? string . Empty ,
106
+ RouteOptions . Version ?? globalOptions . Version ?? string . Empty ,
107
+ ServiceName ?? GetServiceName ( RouteOptions . PluralizeServiceName ?? globalOptions . PluralizeServiceName ?? false ?
108
+ pluralizationService :
97
109
null )
98
110
} ;
99
111
@@ -112,34 +124,38 @@ private string GetServiceName(PluralizationService? pluralizationService)
112
124
[ System . Diagnostics . CodeAnalysis . ExcludeFromCodeCoverage ]
113
125
protected virtual string GetMethodName ( MethodInfo methodInfo , string methodName , ServiceRouteOptions globalOptions )
114
126
{
115
- if ( ! ( Route . AutoAppendId ?? globalOptions . AutoAppendId ?? false ) )
116
- return ServiceBaseHelper . TrimMethodName ( methodName ) ;
127
+ if ( ! ( RouteOptions . AutoAppendId ?? globalOptions . AutoAppendId ?? false ) )
128
+ return ServiceBaseHelper . TrimEndMethodName ( methodName ) ;
117
129
118
130
var idParameter = methodInfo . GetParameters ( ) . FirstOrDefault ( p => p . Name ! . Equals ( "id" , StringComparison . OrdinalIgnoreCase ) ) ;
119
131
if ( idParameter != null )
120
132
{
121
133
var id = idParameter . ParameterType . IsNullableType ( ) || idParameter . HasDefaultValue ? "{id?}" : "{id}" ;
122
- return $ "{ ServiceBaseHelper . TrimMethodName ( methodName ) } /{ id } ";
134
+ return $ "{ ServiceBaseHelper . TrimEndMethodName ( methodName ) } /{ id } ";
123
135
}
124
136
125
- return ServiceBaseHelper . TrimMethodName ( methodName ) ;
137
+ return ServiceBaseHelper . TrimEndMethodName ( methodName ) ;
126
138
}
127
139
128
- protected virtual string ? TryGetHttpMethod ( ServiceRouteOptions globalOptions , ref string methodName )
140
+ protected virtual ( string ? HttpMethod , string MethodName ) ParseMethod ( ServiceRouteOptions globalOptions , string methodName )
129
141
{
130
- if ( ServiceBaseHelper . TryParseHttpMethod ( Route . GetPrefixs ?? globalOptions . GetPrefixs ! , ref methodName ) )
131
- return "GET" ;
142
+ var prefix = ServiceBaseHelper . ParseMethodPrefix ( RouteOptions . GetPrefixes ?? globalOptions . GetPrefixes ! , methodName ) ;
143
+ if ( ! string . IsNullOrEmpty ( prefix ) )
144
+ return ( "GET" , methodName . Substring ( prefix . Length ) ) ;
132
145
133
- if ( ServiceBaseHelper . TryParseHttpMethod ( Route . PostPrefixs ?? globalOptions . PostPrefixs ! , ref methodName ) )
134
- return "POST" ;
146
+ prefix = ServiceBaseHelper . ParseMethodPrefix ( RouteOptions . PostPrefixes ?? globalOptions . PostPrefixes ! , methodName ) ;
147
+ if ( ! string . IsNullOrEmpty ( prefix ) )
148
+ return ( "POST" , methodName . Substring ( prefix . Length ) ) ;
135
149
136
- if ( ServiceBaseHelper . TryParseHttpMethod ( Route . PutPrefixs ?? globalOptions . PutPrefixs ! , ref methodName ) )
137
- return "PUT" ;
150
+ prefix = ServiceBaseHelper . ParseMethodPrefix ( RouteOptions . PutPrefixes ?? globalOptions . PutPrefixes ! , methodName ) ;
151
+ if ( ! string . IsNullOrEmpty ( prefix ) )
152
+ return ( "PUT" , methodName . Substring ( prefix . Length ) ) ;
138
153
139
- if ( ServiceBaseHelper . TryParseHttpMethod ( Route . DeletePrefixs ?? globalOptions . DeletePrefixs ! , ref methodName ) )
140
- return "DELETE" ;
154
+ prefix = ServiceBaseHelper . ParseMethodPrefix ( RouteOptions . DeletePrefixes ?? globalOptions . DeletePrefixes ! , methodName ) ;
155
+ if ( ! string . IsNullOrEmpty ( prefix ) )
156
+ return ( "DELETE" , methodName . Substring ( prefix . Length ) ) ;
141
157
142
- return null ;
158
+ return ( null , string . Empty ) ;
143
159
}
144
160
145
161
#region Obsolete
@@ -171,7 +187,7 @@ protected ServiceBase(IServiceCollection services, string baseUri) : this(servic
171
187
[ System . Diagnostics . CodeAnalysis . ExcludeFromCodeCoverage ]
172
188
protected RouteHandlerBuilder MapGet ( Delegate handler , string ? customUri = null , bool trimEndAsync = true )
173
189
{
174
- customUri ??= ServiceBaseHelper . TrimMethodName ( handler . Method . Name ) ;
190
+ customUri ??= ServiceBaseHelper . TrimEndMethodName ( handler . Method . Name ) ;
175
191
176
192
var pattern = ServiceBaseHelper . CombineUris ( BaseUri , customUri ) ;
177
193
@@ -190,7 +206,7 @@ protected RouteHandlerBuilder MapGet(Delegate handler, string? customUri = null,
190
206
[ System . Diagnostics . CodeAnalysis . ExcludeFromCodeCoverage ]
191
207
protected RouteHandlerBuilder MapPost ( Delegate handler , string ? customUri = null , bool trimEndAsync = true )
192
208
{
193
- customUri ??= ServiceBaseHelper . TrimMethodName ( handler . Method . Name ) ;
209
+ customUri ??= ServiceBaseHelper . TrimEndMethodName ( handler . Method . Name ) ;
194
210
195
211
var pattern = ServiceBaseHelper . CombineUris ( BaseUri , customUri ) ;
196
212
@@ -209,7 +225,7 @@ protected RouteHandlerBuilder MapPost(Delegate handler, string? customUri = null
209
225
[ System . Diagnostics . CodeAnalysis . ExcludeFromCodeCoverage ]
210
226
protected RouteHandlerBuilder MapPut ( Delegate handler , string ? customUri = null , bool trimEndAsync = true )
211
227
{
212
- customUri ??= ServiceBaseHelper . TrimMethodName ( handler . Method . Name ) ;
228
+ customUri ??= ServiceBaseHelper . TrimEndMethodName ( handler . Method . Name ) ;
213
229
214
230
var pattern = ServiceBaseHelper . CombineUris ( BaseUri , customUri ) ;
215
231
@@ -228,7 +244,7 @@ protected RouteHandlerBuilder MapPut(Delegate handler, string? customUri = null,
228
244
[ System . Diagnostics . CodeAnalysis . ExcludeFromCodeCoverage ]
229
245
protected RouteHandlerBuilder MapDelete ( Delegate handler , string ? customUri = null , bool trimEndAsync = true )
230
246
{
231
- customUri ??= ServiceBaseHelper . TrimMethodName ( handler . Method . Name ) ;
247
+ customUri ??= ServiceBaseHelper . TrimEndMethodName ( handler . Method . Name ) ;
232
248
233
249
var pattern = ServiceBaseHelper . CombineUris ( BaseUri , customUri ) ;
234
250
0 commit comments