Skip to content

Commit 2878aac

Browse files
committed
feat(dasherized-route-conv): check if controller is JsonApiController
1 parent 53cc9db commit 2878aac

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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 JsonApiDotNetCore.Controllers;
34
using JsonApiDotNetCore.Extensions;
45
using Microsoft.AspNetCore.Mvc.ApplicationModels;
56

@@ -12,17 +13,28 @@ public DasherizedRoutingConvention(string nspace)
1213
{
1314
_namespace = nspace;
1415
}
15-
16+
1617
public void Apply(ApplicationModel application)
1718
{
1819
foreach (var controller in application.Controllers)
19-
{
20-
var template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
21-
controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel()
20+
{
21+
if (IsJsonApiController(controller))
2222
{
23-
Template = template
24-
};
23+
var template = $"{_namespace}/{controller.ControllerName.Dasherize()}";
24+
controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel()
25+
{
26+
Template = template
27+
};
28+
}
2529
}
2630
}
31+
32+
private bool IsJsonApiController(ControllerModel controller)
33+
{
34+
var controllerBaseType = controller.ControllerType.BaseType;
35+
if(!controllerBaseType.IsConstructedGenericType) return false;
36+
var genericTypeDefinition = controllerBaseType.GetGenericTypeDefinition();
37+
return (genericTypeDefinition == typeof(JsonApiController<,>) || genericTypeDefinition == typeof(JsonApiController<>));
38+
}
2739
}
2840
}

0 commit comments

Comments
 (0)