This repository has been archived by the owner on May 12, 2020. It is now read-only.
forked from OrchardCMS/OrchardCore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutorouteRoute.cs
109 lines (91 loc) · 3.83 KB
/
AutorouteRoute.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.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.DependencyInjection;
using OrchardCore.ContentManagement;
using OrchardCore.ContentManagement.Routing;
namespace OrchardCore.Autoroute.Routing
{
public class AutorouteRoute : IRouter
{
private readonly IAutorouteEntries _entries;
private readonly IRouter _target;
private static HashSet<string> _keys = new HashSet<string>(new[] { "area", "controller", "action", "contentItemId" }, StringComparer.OrdinalIgnoreCase);
public AutorouteRoute(IAutorouteEntries entries, IRouter target)
{
_target = target;
_entries = entries;
}
public VirtualPathData GetVirtualPath(VirtualPathContext context)
{
string contentItemId = context.Values["contentItemId"]?.ToString();
if (string.IsNullOrEmpty(contentItemId))
{
return null;
}
var displayRouteData = GetContentItemDisplayRoutes(context.HttpContext, contentItemId).Result;
if (string.Equals(context.Values["area"]?.ToString(), displayRouteData?["area"]?.ToString(), StringComparison.OrdinalIgnoreCase)
&& string.Equals(context.Values["controller"]?.ToString(), displayRouteData?["controller"]?.ToString(), StringComparison.OrdinalIgnoreCase)
&& string.Equals(context.Values["action"]?.ToString(), displayRouteData?["action"]?.ToString(), StringComparison.OrdinalIgnoreCase))
{
if (_entries.TryGetPath(contentItemId, out string path))
{
if (context.Values.Count > 4)
{
foreach (var data in context.Values)
{
if (!_keys.Contains(data.Key))
{
path = QueryHelpers.AddQueryString(path, data.Key, data.Value.ToString());
}
}
}
return new VirtualPathData(_target, path);
}
}
return null;
}
public async Task RouteAsync(RouteContext context)
{
var requestPath = context.HttpContext.Request.Path.Value;
if (_entries.TryGetContentItemId(requestPath, out var contentItemId))
{
await EnsureRouteData(context, contentItemId);
await _target.RouteAsync(context);
}
}
private async Task<RouteValueDictionary> GetContentItemDisplayRoutes(HttpContext context, string contentItemId)
{
if (string.IsNullOrEmpty(contentItemId))
{
return null;
}
var contentManager = context.RequestServices.GetService<IContentManager>();
var contentItem = await contentManager.GetAsync(contentItemId);
if (contentItem == null)
{
return null;
}
return (await contentManager.PopulateAspectAsync<ContentItemMetadata>(contentItem))?.DisplayRouteValues;
}
private async Task EnsureRouteData(RouteContext context, string contentItemId)
{
var displayRoutes = await GetContentItemDisplayRoutes(context.HttpContext, contentItemId);
if (displayRoutes == null)
{
return;
}
foreach (var key in _keys)
{
if (displayRoutes.ContainsKey(key))
{
context.RouteData.Values[key] = displayRoutes[key];
}
}
context.RouteData.Routers.Add(_target);
}
}
}