1212using JsonApiDotNetCore . Managers . Contracts ;
1313using JsonApiDotNetCore . Models . JsonApiDocuments ;
1414using Microsoft . AspNetCore . Http ;
15+ using Microsoft . AspNetCore . Mvc ;
1516using Microsoft . AspNetCore . Routing ;
1617using Microsoft . Extensions . Primitives ;
1718using Newtonsoft . Json ;
@@ -181,7 +182,7 @@ private static string GetBasePath(string resourceName, IJsonApiOptions options,
181182 builder . Append ( httpRequest . Host ) ;
182183 }
183184
184- string customRoute = GetCustomRoute ( httpRequest . Path . Value , resourceName , options . Namespace ) ;
185+ string customRoute = GetCustomRoute ( resourceName , options . Namespace , httpRequest . HttpContext ) ;
185186 if ( ! string . IsNullOrEmpty ( customRoute ) )
186187 {
187188 builder . Append ( '/' ) ;
@@ -196,13 +197,20 @@ private static string GetBasePath(string resourceName, IJsonApiOptions options,
196197 return builder . ToString ( ) ;
197198 }
198199
199- private static string GetCustomRoute ( string path , string resourceName , string apiNamespace )
200+ private static string GetCustomRoute ( string resourceName , string apiNamespace , HttpContext httpContext )
200201 {
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 ;
206214 }
207215
208216 private static bool GetIsRelationshipPath ( RouteValueDictionary routeValues )
0 commit comments