11// REF: https://github.com/aspnet/Entropy/blob/dev/samples/Mvc.CustomRoutingConvention/NameSpaceRoutingConvention.cs
22// REF: https://github.com/aspnet/Mvc/issues/5691
3+ using System . Reflection ;
34using JsonApiDotNetCore . Controllers ;
45using JsonApiDotNetCore . Extensions ;
6+ using Microsoft . AspNetCore . Mvc ;
57using Microsoft . AspNetCore . Mvc . ApplicationModels ;
68
79namespace JsonApiDotNetCore . Internal
@@ -17,21 +19,36 @@ public DasherizedRoutingConvention(string nspace)
1719 public void Apply ( ApplicationModel application )
1820 {
1921 foreach ( var controller in application . Controllers )
20- {
21- if ( IsJsonApiController ( controller ) )
22+ {
23+ var template = string . Empty ;
24+
25+ if ( IsDasherizedJsonApiController ( controller ) )
26+ template = $ "{ _namespace } /{ controller . ControllerName . Dasherize ( ) } ";
27+ else
28+ template = GetTemplate ( controller ) ;
29+
30+ controller . Selectors [ 0 ] . AttributeRouteModel = new AttributeRouteModel ( )
2231 {
23- var template = $ "{ _namespace } /{ controller . ControllerName . Dasherize ( ) } ";
24- controller . Selectors [ 0 ] . AttributeRouteModel = new AttributeRouteModel ( )
25- {
26- Template = template
27- } ;
28- }
32+ Template = template
33+ } ;
2934 }
3035 }
3136
32- private bool IsJsonApiController ( ControllerModel controller )
37+ private bool IsDasherizedJsonApiController ( ControllerModel controller )
3338 {
34- return controller . ControllerType . IsSubclassOf ( typeof ( JsonApiControllerMixin ) ) ;
39+ var type = controller . ControllerType ;
40+ var notDisabled = type . GetCustomAttribute < DisableRoutingConventionAttribute > ( ) == null ;
41+ return notDisabled && type . IsSubclassOf ( typeof ( JsonApiControllerMixin ) ) ;
42+ }
43+
44+ private string GetTemplate ( ControllerModel controller )
45+ {
46+ var type = controller . ControllerType ;
47+ var routeAttr = type . GetCustomAttribute < RouteAttribute > ( ) ;
48+ if ( routeAttr != null )
49+ return ( ( RouteAttribute ) routeAttr ) . Template ;
50+
51+ return controller . ControllerName ;
3552 }
3653 }
3754}
0 commit comments