This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
/
UrlHelperBase.cs
466 lines (407 loc) · 15.8 KB
/
UrlHelperBase.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
// 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.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Routing;
namespace Microsoft.AspNetCore.Mvc.Routing
{
public abstract class UrlHelperBase : IUrlHelper
{
// Perf: Share the StringBuilder object across multiple calls of GenerateURL for this UrlHelper
private StringBuilder _stringBuilder;
// Perf: Reuse the RouteValueDictionary across multiple calls of Action for this UrlHelper
private readonly RouteValueDictionary _routeValueDictionary;
protected UrlHelperBase(ActionContext actionContext)
{
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
ActionContext = actionContext;
AmbientValues = actionContext.RouteData.Values;
_routeValueDictionary = new RouteValueDictionary();
}
/// <summary>
/// Gets the <see cref="RouteValueDictionary"/> associated with the current request.
/// </summary>
protected RouteValueDictionary AmbientValues { get; }
/// <inheritdoc />
public ActionContext ActionContext { get; }
/// <inheritdoc />
public virtual bool IsLocalUrl(string url)
{
if (string.IsNullOrEmpty(url))
{
return false;
}
// Allows "/" or "/foo" but not "//" or "/\".
if (url[0] == '/')
{
// url is exactly "/"
if (url.Length == 1)
{
return true;
}
// url doesn't start with "//" or "/\"
if (url[1] != '/' && url[1] != '\\')
{
return true;
}
return false;
}
// Allows "~/" or "~/foo" but not "~//" or "~/\".
if (url[0] == '~' && url.Length > 1 && url[1] == '/')
{
// url is exactly "~/"
if (url.Length == 2)
{
return true;
}
// url doesn't start with "~//" or "~/\"
if (url[2] != '/' && url[2] != '\\')
{
return true;
}
return false;
}
return false;
}
/// <inheritdoc />
public virtual string Content(string contentPath)
{
if (string.IsNullOrEmpty(contentPath))
{
return null;
}
else if (contentPath[0] == '~')
{
var segment = new PathString(contentPath.Substring(1));
var applicationPath = ActionContext.HttpContext.Request.PathBase;
return applicationPath.Add(segment).Value;
}
return contentPath;
}
/// <inheritdoc />
public virtual string Link(string routeName, object values)
{
return RouteUrl(new UrlRouteContext()
{
RouteName = routeName,
Values = values,
Protocol = ActionContext.HttpContext.Request.Scheme,
Host = ActionContext.HttpContext.Request.Host.ToUriComponent()
});
}
/// <inheritdoc />
public abstract string Action(UrlActionContext actionContext);
/// <inheritdoc />
public abstract string RouteUrl(UrlRouteContext routeContext);
protected RouteValueDictionary GetValuesDictionary(object values)
{
// Perf: RouteValueDictionary can be cast to IDictionary<string, object>, but it is
// special cased to avoid allocating boxed Enumerator.
if (values is RouteValueDictionary routeValuesDictionary)
{
_routeValueDictionary.Clear();
foreach (var kvp in routeValuesDictionary)
{
_routeValueDictionary.Add(kvp.Key, kvp.Value);
}
return _routeValueDictionary;
}
if (values is IDictionary<string, object> dictionaryValues)
{
_routeValueDictionary.Clear();
foreach (var kvp in dictionaryValues)
{
_routeValueDictionary.Add(kvp.Key, kvp.Value);
}
return _routeValueDictionary;
}
return new RouteValueDictionary(values);
}
protected string GenerateUrl(string protocol, string host, string virtualPath, string fragment)
{
if (virtualPath == null)
{
return null;
}
// Perf: In most of the common cases, GenerateUrl is called with a null protocol, host and fragment.
// In such cases, we might not need to build any URL as the url generated is mostly same as the virtual path available in pathData.
// For such common cases, this FastGenerateUrl method saves a string allocation per GenerateUrl call.
if (TryFastGenerateUrl(protocol, host, virtualPath, fragment, out var url))
{
return url;
}
var builder = GetStringBuilder();
try
{
var pathBase = ActionContext.HttpContext.Request.PathBase;
if (string.IsNullOrEmpty(protocol) && string.IsNullOrEmpty(host))
{
AppendPathAndFragment(builder, pathBase, virtualPath, fragment);
// We're returning a partial URL (just path + query + fragment), but we still want it to be rooted.
if (builder.Length == 0 || builder[0] != '/')
{
builder.Insert(0, '/');
}
}
else
{
protocol = string.IsNullOrEmpty(protocol) ? "http" : protocol;
builder.Append(protocol);
builder.Append("://");
host = string.IsNullOrEmpty(host) ? ActionContext.HttpContext.Request.Host.Value : host;
builder.Append(host);
AppendPathAndFragment(builder, pathBase, virtualPath, fragment);
}
var path = builder.ToString();
return path;
}
finally
{
// Clear the StringBuilder so that it can reused for the next call.
builder.Clear();
}
}
/// <summary>
/// Generates a URI from the provided components.
/// </summary>
/// <param name="protocol">The URI scheme/protocol.</param>
/// <param name="host">The URI host.</param>
/// <param name="path">The URI path and remaining portions (path, query, and fragment).</param>
/// <returns>
/// An absolute URI if the <paramref name="protocol"/> or <paramref name="host"/> is specified, otherwise generates a
/// URI with an absolute path.
/// </returns>
protected string GenerateUrl(string protocol, string host, string path)
{
// This method is similar to GenerateUrl, but it's used for EndpointRouting. It ignores pathbase and fragment
// because those have already been incorporated.
if (path == null)
{
return null;
}
// Perf: In most of the common cases, GenerateUrl is called with a null protocol, host and fragment.
// In such cases, we might not need to build any URL as the url generated is mostly same as the virtual path available in pathData.
// For such common cases, this FastGenerateUrl method saves a string allocation per GenerateUrl call.
if (TryFastGenerateUrl(protocol, host, path, fragment: null, out var url))
{
return url;
}
var builder = GetStringBuilder();
try
{
if (string.IsNullOrEmpty(protocol) && string.IsNullOrEmpty(host))
{
AppendPathAndFragment(builder, pathBase: null, path, fragment: null);
// We're returning a partial URL (just path + query + fragment), but we still want it to be rooted.
if (builder.Length == 0 || builder[0] != '/')
{
builder.Insert(0, '/');
}
}
else
{
protocol = string.IsNullOrEmpty(protocol) ? "http" : protocol;
builder.Append(protocol);
builder.Append("://");
host = string.IsNullOrEmpty(host) ? ActionContext.HttpContext.Request.Host.Value : host;
builder.Append(host);
AppendPathAndFragment(builder, pathBase: null, path, fragment: null);
}
return builder.ToString();
}
finally
{
// Clear the StringBuilder so that it can reused for the next call.
builder.Clear();
}
}
internal static void NormalizeRouteValuesForAction(
string action,
string controller,
RouteValueDictionary values,
RouteValueDictionary ambientValues)
{
object obj = null;
if (action == null)
{
if (!values.ContainsKey("action") &&
(ambientValues?.TryGetValue("action", out obj) ?? false))
{
values["action"] = obj;
}
}
else
{
values["action"] = action;
}
if (controller == null)
{
if (!values.ContainsKey("controller") &&
(ambientValues?.TryGetValue("controller", out obj) ?? false))
{
values["controller"] = obj;
}
}
else
{
values["controller"] = controller;
}
}
internal static void NormalizeRouteValuesForPage(
ActionContext context,
string page,
string handler,
RouteValueDictionary values,
RouteValueDictionary ambientValues)
{
object value = null;
if (string.IsNullOrEmpty(page))
{
if (!values.ContainsKey("page") &&
(ambientValues?.TryGetValue("page", out value) ?? false))
{
values["page"] = value;
}
}
else
{
values["page"] = CalculatePageName(context, ambientValues, page);
}
if (string.IsNullOrEmpty(handler))
{
if (!values.ContainsKey("handler") &&
(ambientValues?.ContainsKey("handler") ?? false))
{
// Clear out form action unless it's explicitly specified in the routeValues.
values["handler"] = null;
}
}
else
{
values["handler"] = handler;
}
}
private static object CalculatePageName(ActionContext context, RouteValueDictionary ambientValues, string pageName)
{
Debug.Assert(pageName.Length > 0);
// Paths not qualified with a leading slash are treated as relative to the current page.
if (pageName[0] != '/')
{
// OK now we should get the best 'normalized' version of the page route value that we can.
string currentPagePath;
if (context != null)
{
currentPagePath = NormalizedRouteValue.GetNormalizedRouteValue(context, "page");
}
else if (ambientValues != null)
{
currentPagePath = Convert.ToString(ambientValues["page"], CultureInfo.InvariantCulture);
}
else
{
currentPagePath = null;
}
if (string.IsNullOrEmpty(currentPagePath))
{
// Disallow the use sibling page routing, a Razor page specific feature, from a non-page action.
// OR - this is a call from LinkGenerator where the HttpContext was not specified.
//
// We can't use a relative path in either case, because we don't know the base path.
throw new InvalidOperationException(Resources.FormatUrlHelper_RelativePagePathIsNotSupported(
pageName,
nameof(LinkGenerator),
nameof(HttpContext)));
}
return ViewEnginePath.CombinePath(currentPagePath, pageName);
}
return pageName;
}
// for unit testing
internal static void AppendPathAndFragment(StringBuilder builder, PathString pathBase, string virtualPath, string fragment)
{
if (!pathBase.HasValue)
{
if (virtualPath.Length == 0)
{
builder.Append("/");
}
else
{
if (!virtualPath.StartsWith("/", StringComparison.Ordinal))
{
builder.Append("/");
}
builder.Append(virtualPath);
}
}
else
{
if (virtualPath.Length == 0)
{
builder.Append(pathBase.Value);
}
else
{
builder.Append(pathBase.Value);
if (pathBase.Value.EndsWith("/", StringComparison.Ordinal))
{
builder.Length--;
}
if (!virtualPath.StartsWith("/", StringComparison.Ordinal))
{
builder.Append("/");
}
builder.Append(virtualPath);
}
}
if (!string.IsNullOrEmpty(fragment))
{
builder.Append("#").Append(fragment);
}
}
private bool TryFastGenerateUrl(
string protocol,
string host,
string virtualPath,
string fragment,
out string url)
{
var pathBase = ActionContext.HttpContext.Request.PathBase;
url = null;
if (string.IsNullOrEmpty(protocol)
&& string.IsNullOrEmpty(host)
&& string.IsNullOrEmpty(fragment)
&& !pathBase.HasValue)
{
if (virtualPath.Length == 0)
{
url = "/";
return true;
}
else if (virtualPath.StartsWith("/", StringComparison.Ordinal))
{
url = virtualPath;
return true;
}
}
return false;
}
private StringBuilder GetStringBuilder()
{
if (_stringBuilder == null)
{
_stringBuilder = new StringBuilder();
}
return _stringBuilder;
}
}
}