This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 126
/
VirtualPathData.cs
99 lines (87 loc) · 2.99 KB
/
VirtualPathData.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
// 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;
namespace Microsoft.AspNetCore.Routing
{
/// <summary>
/// Represents information about the route and virtual path that are the result of
/// generating a URL with the ASP.NET routing middleware.
/// </summary>
public class VirtualPathData
{
private RouteValueDictionary _dataTokens;
private string _virtualPath;
/// <summary>
/// Initializes a new instance of the <see cref="VirtualPathData"/> class.
/// </summary>
/// <param name="router">The object that is used to generate the URL.</param>
/// <param name="virtualPath">The generated URL.</param>
public VirtualPathData(IRouter router, string virtualPath)
: this(router, virtualPath, dataTokens: null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="VirtualPathData"/> class.
/// </summary>
/// <param name="router">The object that is used to generate the URL.</param>
/// <param name="virtualPath">The generated URL.</param>
/// <param name="dataTokens">The collection of custom values.</param>
public VirtualPathData(
IRouter router,
string virtualPath,
RouteValueDictionary dataTokens)
{
if (router == null)
{
throw new ArgumentNullException(nameof(router));
}
Router = router;
VirtualPath = virtualPath;
_dataTokens = dataTokens == null ? null : new RouteValueDictionary(dataTokens);
}
/// <summary>
/// Gets the collection of custom values for the <see cref="Router"/>.
/// </summary>
public RouteValueDictionary DataTokens
{
get
{
if (_dataTokens == null)
{
_dataTokens = new RouteValueDictionary();
}
return _dataTokens;
}
}
/// <summary>
/// Gets or sets the <see cref="IRouter"/> that was used to generate the URL.
/// </summary>
public IRouter Router { get; set; }
/// <summary>
/// Gets or sets the URL that was generated from the <see cref="Router"/>.
/// </summary>
public string VirtualPath
{
get
{
return _virtualPath;
}
set
{
_virtualPath = NormalizePath(value);
}
}
private static string NormalizePath(string path)
{
if (string.IsNullOrEmpty(path))
{
return string.Empty;
}
if (!path.StartsWith("/", StringComparison.Ordinal))
{
return "/" + path;
}
return path;
}
}
}