forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamespaceHttpControllerSelector.cs
More file actions
63 lines (54 loc) · 2.46 KB
/
NamespaceHttpControllerSelector.cs
File metadata and controls
63 lines (54 loc) · 2.46 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
58
59
60
61
62
63
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using System.Web.Http.Routing;
namespace SiteServer.API
{
public class NamespaceHttpControllerSelector : IHttpControllerSelector
{
private readonly HttpConfiguration _configuration;
private readonly Lazy<Dictionary<string, HttpControllerDescriptor>> _controllers;
public NamespaceHttpControllerSelector(HttpConfiguration config)
{
_configuration = config;
_controllers = new Lazy<Dictionary<string, HttpControllerDescriptor>>(InitializeControllerDictionary);
}
private Dictionary<string, HttpControllerDescriptor> InitializeControllerDictionary()
{
var dictionary = new Dictionary<string, HttpControllerDescriptor>(StringComparer.OrdinalIgnoreCase);
var assembliesResolver = _configuration.Services.GetAssembliesResolver();
var controllersResolver = _configuration.Services.GetHttpControllerTypeResolver();
var controllerTypes = controllersResolver.GetControllerTypes(assembliesResolver);
foreach (var t in controllerTypes)
{
dictionary[$"{t.Namespace}.{t.Name}"] = new HttpControllerDescriptor(_configuration, t.Name, t);
}
return dictionary;
}
public HttpControllerDescriptor SelectController(HttpRequestMessage request)
{
var attributedRoutesData = request.GetRouteData().GetSubRoutes();
var subRouteData = attributedRoutesData.FirstOrDefault();
if (subRouteData != null)
{
return ((HttpActionDescriptor[]) subRouteData.Route.DataTokens["actions"])[0].ControllerDescriptor;
//var controllerType = ((HttpActionDescriptor[])subRouteData.Route.DataTokens["actions"])[0].ControllerDescriptor.ControllerType;
//if (_controllers.Value.TryGetValue($"{controllerType.Namespace}.{controllerType.Name}",
// out var controllerDescriptor))
//{
// return controllerDescriptor;
//}
}
throw new HttpResponseException(HttpStatusCode.NotFound);
}
public IDictionary<string, HttpControllerDescriptor> GetControllerMapping()
{
return _controllers.Value;
}
}
}