/
ControllerActionSearcher.cs
109 lines (94 loc) · 4.01 KB
/
ControllerActionSearcher.cs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Web.Common.Controllers;
using static Umbraco.Cms.Core.Constants.Web.Routing;
namespace Umbraco.Cms.Web.Website.Routing
{
/// <summary>
/// Used to find a controller/action in the current available routes
/// </summary>
public class ControllerActionSearcher : IControllerActionSearcher
{
private readonly ILogger<ControllerActionSearcher> _logger;
private readonly IActionSelector _actionSelector;
private const string DefaultActionName = nameof(RenderController.Index);
/// <summary>
/// Initializes a new instance of the <see cref="ControllerActionSearcher"/> class.
/// </summary>
public ControllerActionSearcher(
ILogger<ControllerActionSearcher> logger,
IActionSelector actionSelector)
{
_logger = logger;
_actionSelector = actionSelector;
}
/// <summary>
/// Determines if a custom controller can hijack the current route
/// </summary>
/// <typeparam name="T">The controller type to find</typeparam>
public ControllerActionDescriptor Find<T>(HttpContext httpContext, string controller, string action) => Find<T>(httpContext, controller, action, null);
/// <summary>
/// Determines if a custom controller can hijack the current route
/// </summary>
/// <typeparam name="T">The controller type to find</typeparam>
public ControllerActionDescriptor Find<T>(HttpContext httpContext, string controller, string action, string area)
{
IReadOnlyList<ControllerActionDescriptor> candidates = FindControllerCandidates<T>(httpContext, controller, action, DefaultActionName, area);
if (candidates.Count > 0)
{
return candidates[0];
}
return null;
}
/// <summary>
/// Return a list of controller candidates that match the custom controller and action names
/// </summary>
private IReadOnlyList<ControllerActionDescriptor> FindControllerCandidates<T>(
HttpContext httpContext,
string customControllerName,
string customActionName,
string defaultActionName,
string area = null)
{
// Use aspnetcore's IActionSelector to do the finding since it uses an optimized cache lookup
var routeValues = new RouteValueDictionary
{
[ControllerToken] = customControllerName,
[ActionToken] = customActionName, // first try to find the custom action
};
if (area != null)
{
routeValues[AreaToken] = area;
}
var routeData = new RouteData(routeValues);
var routeContext = new RouteContext(httpContext)
{
RouteData = routeData
};
// try finding candidates for the custom action
var candidates = _actionSelector.SelectCandidates(routeContext)
.Cast<ControllerActionDescriptor>()
.Where(x => TypeHelper.IsTypeAssignableFrom<T>(x.ControllerTypeInfo))
.ToList();
if (candidates.Count > 0)
{
// return them if found
return candidates;
}
// now find for the default action since we couldn't find the custom one
routeValues[ActionToken] = defaultActionName;
candidates = _actionSelector.SelectCandidates(routeContext)
.Cast<ControllerActionDescriptor>()
.Where(x => TypeHelper.IsTypeAssignableFrom<T>(x.ControllerTypeInfo))
.ToList();
return candidates;
}
}
}