This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
PageRouteModelFactory.cs
190 lines (161 loc) · 8.49 KB
/
PageRouteModelFactory.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
namespace Microsoft.AspNetCore.Mvc.RazorPages.Internal
{
internal class PageRouteModelFactory
{
private static readonly string IndexFileName = "Index" + RazorViewEngine.ViewExtension;
private readonly RazorPagesOptions _options;
private readonly ILogger _logger;
private readonly string _normalizedRootDirectory;
private readonly string _normalizedAreaRootDirectory;
public PageRouteModelFactory(
RazorPagesOptions options,
ILogger logger)
{
_options = options ?? throw new ArgumentNullException(nameof(options));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_normalizedRootDirectory = NormalizeDirectory(options.RootDirectory);
_normalizedAreaRootDirectory = NormalizeDirectory(options.AreaRootDirectory);
}
public PageRouteModel CreateRouteModel(string relativePath, string routeTemplate)
{
var viewEnginePath = GetViewEnginePath(_normalizedRootDirectory, relativePath);
var routeModel = new PageRouteModel(relativePath, viewEnginePath);
PopulateRouteModel(routeModel, viewEnginePath, routeTemplate);
return routeModel;
}
public PageRouteModel CreateAreaRouteModel(string relativePath, string routeTemplate)
{
if (!TryParseAreaPath(relativePath, out var areaResult))
{
return null;
}
var routeModel = new PageRouteModel(relativePath, areaResult.viewEnginePath, areaResult.areaName);
var routePrefix = CreateAreaRoute(areaResult.areaName, areaResult.viewEnginePath);
PopulateRouteModel(routeModel, routePrefix, routeTemplate);
routeModel.RouteValues["area"] = areaResult.areaName;
return routeModel;
}
private static void PopulateRouteModel(PageRouteModel model, string pageRoute, string routeTemplate)
{
model.RouteValues.Add("page", model.ViewEnginePath);
var selectorModel = CreateSelectorModel(pageRoute, routeTemplate);
model.Selectors.Add(selectorModel);
var fileName = Path.GetFileName(model.RelativePath);
if (!AttributeRouteModel.IsOverridePattern(routeTemplate) &&
string.Equals(IndexFileName, fileName, StringComparison.OrdinalIgnoreCase))
{
// For pages without an override route, and ending in /Index.cshtml, we want to allow
// incoming routing, but force outgoing routes to match to the path sans /Index.
selectorModel.AttributeRouteModel.SuppressLinkGeneration = true;
var index = pageRoute.LastIndexOf('/');
var parentDirectoryPath = index == -1 ?
string.Empty :
pageRoute.Substring(0, index);
model.Selectors.Add(CreateSelectorModel(parentDirectoryPath, routeTemplate));
}
}
// Internal for unit testing
internal bool TryParseAreaPath(
string relativePath,
out (string areaName, string viewEnginePath) result)
{
// path = "/Areas/Products/Pages/Manage/Home.cshtml"
// Result ("Products", "/Manage/Home")
result = default;
Debug.Assert(relativePath.StartsWith("/", StringComparison.Ordinal));
// Parse the area root directory.
var areaRootEndIndex = relativePath.IndexOf('/', startIndex: 1);
if (areaRootEndIndex == -1 ||
areaRootEndIndex >= relativePath.Length - 1 || // There's at least one token after the area root.
!relativePath.StartsWith(_normalizedAreaRootDirectory, StringComparison.OrdinalIgnoreCase)) // The path must start with area root.
{
_logger.UnsupportedAreaPath(_options, relativePath);
return false;
}
// The first directory that follows the area root is the area name.
var areaEndIndex = relativePath.IndexOf('/', startIndex: areaRootEndIndex + 1);
if (areaEndIndex == -1 || areaEndIndex == relativePath.Length)
{
_logger.UnsupportedAreaPath(_options, relativePath);
return false;
}
var areaName = relativePath.Substring(areaRootEndIndex + 1, areaEndIndex - areaRootEndIndex - 1);
string viewEnginePath;
if (_options.RootDirectory == "/")
{
// When RootDirectory is "/", every thing past the area name is the page path.
Debug.Assert(relativePath.EndsWith(RazorViewEngine.ViewExtension), $"{relativePath} does not end in extension '{RazorViewEngine.ViewExtension}'.");
viewEnginePath = relativePath.Substring(areaEndIndex, relativePath.Length - areaEndIndex - RazorViewEngine.ViewExtension.Length);
}
else
{
// Normalize the pages root directory so that it has a trailing slash. This ensures we're looking at a directory delimiter
// and not just the area name occuring as part of a segment.
Debug.Assert(_options.RootDirectory.StartsWith("/", StringComparison.Ordinal));
// If the pages root has a value i.e. it's not the app root "/", ensure that the area path contains this value.
if (string.Compare(relativePath, areaEndIndex, _normalizedRootDirectory, 0, _normalizedRootDirectory.Length, StringComparison.OrdinalIgnoreCase) != 0)
{
_logger.UnsupportedAreaPath(_options, relativePath);
return false;
}
// Include the trailing slash of the root directory at the start of the viewEnginePath
var pageNameIndex = areaEndIndex + _normalizedRootDirectory.Length - 1;
viewEnginePath = relativePath.Substring(pageNameIndex, relativePath.Length - pageNameIndex - RazorViewEngine.ViewExtension.Length);
}
result = (areaName, viewEnginePath);
return true;
}
private string GetViewEnginePath(string rootDirectory, string path)
{
// rootDirectory = "/Pages/AllMyPages/"
// path = "/Pages/AllMyPages/Home.cshtml"
// Result = "/Home"
Debug.Assert(path.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase));
Debug.Assert(path.EndsWith(RazorViewEngine.ViewExtension, StringComparison.OrdinalIgnoreCase));
var startIndex = rootDirectory.Length - 1;
var endIndex = path.Length - RazorViewEngine.ViewExtension.Length;
return path.Substring(startIndex, endIndex - startIndex);
}
private static string CreateAreaRoute(string areaName, string viewEnginePath)
{
// AreaName = Products, ViewEnginePath = /List/Categories
// Result = /Products/List/Categories
Debug.Assert(!string.IsNullOrEmpty(areaName));
Debug.Assert(!string.IsNullOrEmpty(viewEnginePath));
Debug.Assert(viewEnginePath.StartsWith("/", StringComparison.Ordinal));
var builder = new InplaceStringBuilder(1 + areaName.Length + viewEnginePath.Length);
builder.Append('/');
builder.Append(areaName);
builder.Append(viewEnginePath);
return builder.ToString();
}
private static SelectorModel CreateSelectorModel(string prefix, string routeTemplate)
{
return new SelectorModel
{
AttributeRouteModel = new AttributeRouteModel
{
Template = AttributeRouteModel.CombineTemplates(prefix, routeTemplate),
}
};
}
private static string NormalizeDirectory(string directory)
{
Debug.Assert(directory.StartsWith("/", StringComparison.Ordinal));
if (directory.Length > 1 && !directory.EndsWith("/", StringComparison.Ordinal))
{
return directory + "/";
}
return directory;
}
}
}