-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGenericControllerAttributeConvention.cs
More file actions
30 lines (28 loc) · 1.01 KB
/
Copy pathGenericControllerAttributeConvention.cs
File metadata and controls
30 lines (28 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Reflection;
namespace dotnet_roslyn_dynamic_api
{
public class GenericControllerRouteConvention : IControllerModelConvention
{
public void Apply(ControllerModel controller)
{
if (controller.ControllerType.IsGenericType)
{
var genericType = controller.ControllerType.GenericTypeArguments[0];
var customNameAttribute = genericType.GetCustomAttribute<GeneratedControllerAttribute>();
if (customNameAttribute?.Route != null)
{
controller.Selectors.Add(new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel(new RouteAttribute(customNameAttribute.Route)),
});
}
else
{
controller.ControllerName = genericType.Name;
}
}
}
}
}