-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncludeNestedControllers.cs
More file actions
57 lines (47 loc) · 1.65 KB
/
IncludeNestedControllers.cs
File metadata and controls
57 lines (47 loc) · 1.65 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
namespace Microsoft.AspNetCore.Mvc.Controllers;
/// <summary>
/// Discovers controllers whose access modifiers are public but nested inside other public classes, which the default
/// <see cref="ControllerFeatureProvider"/> would miss out.
/// </summary>
public class NestedControllerFeatureProvider : ControllerFeatureProvider
{
private const string ControllerTypeNameSuffix = "Controller";
private readonly bool _includeNestedControllers;
public NestedControllerFeatureProvider(bool includeNestedControllers = false)
{
_includeNestedControllers = includeNestedControllers;
}
protected override bool IsController(TypeInfo typeInfo)
{
if (!_includeNestedControllers)
return base.IsController(typeInfo);
if (!typeInfo.IsClass)
{
return false;
}
if (typeInfo.IsAbstract)
{
return false;
}
// IsPublic returns false for nested classes, regardless of visibility modifiers.
// We wish to include nested controllers, so we only return false if IsPublic is false AND IsNestedPublic is false.
if (!typeInfo.IsPublic && !typeInfo.IsNestedPublic)
{
return false;
}
if (typeInfo.ContainsGenericParameters)
{
return false;
}
if (typeInfo.IsDefined(typeof(NonControllerAttribute)))
{
return false;
}
if (!typeInfo.Name.EndsWith(ControllerTypeNameSuffix, StringComparison.OrdinalIgnoreCase) &&
!typeInfo.IsDefined(typeof(ControllerAttribute)))
{
return false;
}
return true;
}
}