12
12
using JsonApiDotNetCore . Managers . Contracts ;
13
13
using JsonApiDotNetCore . Models . JsonApiDocuments ;
14
14
using Microsoft . AspNetCore . Http ;
15
+ using Microsoft . AspNetCore . Mvc ;
15
16
using Microsoft . AspNetCore . Routing ;
16
17
using Microsoft . Extensions . Primitives ;
17
18
using Newtonsoft . Json ;
@@ -181,7 +182,7 @@ private static string GetBasePath(string resourceName, IJsonApiOptions options,
181
182
builder . Append ( httpRequest . Host ) ;
182
183
}
183
184
184
- string customRoute = GetCustomRoute ( httpRequest . Path . Value , resourceName , options . Namespace ) ;
185
+ string customRoute = GetCustomRoute ( resourceName , options . Namespace , httpRequest . HttpContext ) ;
185
186
if ( ! string . IsNullOrEmpty ( customRoute ) )
186
187
{
187
188
builder . Append ( '/' ) ;
@@ -196,13 +197,20 @@ private static string GetBasePath(string resourceName, IJsonApiOptions options,
196
197
return builder . ToString ( ) ;
197
198
}
198
199
199
- private static string GetCustomRoute ( string path , string resourceName , string apiNamespace )
200
+ private static string GetCustomRoute ( string resourceName , string apiNamespace , HttpContext httpContext )
200
201
{
201
- var trimmedComponents = path . Trim ( '/' ) . Split ( '/' ) . ToList ( ) ;
202
- var resourceNameIndex = trimmedComponents . FindIndex ( c => c == resourceName ) ;
203
- var newComponents = trimmedComponents . Take ( resourceNameIndex ) . ToArray ( ) ;
204
- var customRoute = string . Join ( '/' , newComponents ) ;
205
- return customRoute == apiNamespace ? null : customRoute ;
202
+ var endpoint = httpContext . GetEndpoint ( ) ;
203
+ var routeAttribute = endpoint . Metadata . GetMetadata < RouteAttribute > ( ) ;
204
+ if ( routeAttribute != null )
205
+ {
206
+ var trimmedComponents = httpContext . Request . Path . Value . Trim ( '/' ) . Split ( '/' ) . ToList ( ) ;
207
+ var resourceNameIndex = trimmedComponents . FindIndex ( c => c == resourceName ) ;
208
+ var newComponents = trimmedComponents . Take ( resourceNameIndex ) . ToArray ( ) ;
209
+ var customRoute = string . Join ( '/' , newComponents ) ;
210
+ return customRoute == apiNamespace ? null : customRoute ;
211
+ }
212
+
213
+ return null ;
206
214
}
207
215
208
216
private static bool GetIsRelationshipPath ( RouteValueDictionary routeValues )
0 commit comments