forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUrlHelper.cs
More file actions
209 lines (186 loc) · 8.12 KB
/
Copy pathUrlHelper.cs
File metadata and controls
209 lines (186 loc) · 8.12 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
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
// 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.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Properties;
namespace System.Web.Http.Routing
{
/// <summary>
/// Represents a factory for creating URLs.
/// </summary>
public class UrlHelper
{
private HttpRequestMessage _request;
/// <summary>
/// Initializes a new instance of the <see cref="UrlHelper"/> class.
/// </summary>
/// <remarks>The default constructor is intended for use by unit testing only.</remarks>
public UrlHelper()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="UrlHelper"/> class.
/// </summary>
/// <param name="request">The HTTP request message containing the context under which the URLs are generated.</param>
public UrlHelper(HttpRequestMessage request)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
Request = request;
}
/// <summary>
/// Gets the <see cref="HttpRequestMessage"/> of the current <see cref="UrlHelper"/>.
/// The setter is not intended to be used other than for unit testing purpose.
/// </summary>
public HttpRequestMessage Request
{
get { return _request; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_request = value;
}
}
/// <summary>
/// Creates an absolute URL using the specified path.
/// </summary>
/// <param name="path">The URL path, which may be a relative URL, a rooted URL, or a virtual path.</param>
/// <returns>The generated URL.</returns>
[SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "It is safe to pass string here")]
public virtual string Content(string path)
{
if (String.IsNullOrEmpty(path))
{
throw Error.ArgumentNullOrEmpty("path");
}
if (Request == null)
{
throw Error.InvalidOperation(SRResources.RequestIsNull, "UrlHelper");
}
if (path.StartsWith("~/", StringComparison.Ordinal))
{
// This is a virtual path, we need to combine it with the virtual path root
string virtualPathRoot;
HttpRequestContext requestContext = Request.GetRequestContext();
if (requestContext != null)
{
virtualPathRoot = requestContext.VirtualPathRoot;
}
else
{
HttpConfiguration configuration = Request.GetConfiguration();
if (configuration == null)
{
throw Error.InvalidOperation(SRResources.HttpRequestMessageExtensions_NoConfiguration);
}
virtualPathRoot = configuration.VirtualPathRoot;
}
if (virtualPathRoot == null)
{
virtualPathRoot = "/";
}
if (!virtualPathRoot.StartsWith("/", StringComparison.Ordinal))
{
virtualPathRoot = "/" + virtualPathRoot;
}
if (!virtualPathRoot.EndsWith("/", StringComparison.Ordinal))
{
virtualPathRoot += "/";
}
return new Uri(Request.RequestUri, virtualPathRoot + path.Substring("~/".Length)).AbsoluteUri;
}
else
{
return new Uri(Request.RequestUri, path).AbsoluteUri;
}
}
/// <summary>
/// Creates a relative URL using the specified route and route data.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <returns>The generated URL.</returns>
public virtual string Route(string routeName, object routeValues)
{
return Route(routeName, new HttpRouteValueDictionary(routeValues));
}
/// <summary>
/// Creates a relative URL using the specified route and route data.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <returns>The generated URL.</returns>
public virtual string Route(string routeName, IDictionary<string, object> routeValues)
{
return GetVirtualPath(Request, routeName, routeValues);
}
/// <summary>
/// Creates an absolute URL using the given route and route data.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <returns>The generated URL.</returns>
[SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "It is safe to pass string here")]
public virtual string Link(string routeName, object routeValues)
{
return Link(routeName, new HttpRouteValueDictionary(routeValues));
}
/// <summary>
/// Creates an absolute URL using the specified route and route data.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <returns>The generated URL.</returns>
[SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "It is safe to pass string here")]
public virtual string Link(string routeName, IDictionary<string, object> routeValues)
{
string link = Route(routeName, routeValues);
if (!String.IsNullOrEmpty(link))
{
link = new Uri(Request.RequestUri, link).AbsoluteUri;
}
return link;
}
private static string GetVirtualPath(HttpRequestMessage request, string routeName, IDictionary<string, object> routeValues)
{
if (routeValues == null)
{
// If no route values were passed in at all we have to create a new dictionary
// so that we can add the extra "httproute" key.
routeValues = new HttpRouteValueDictionary();
routeValues.Add(HttpRoute.HttpRouteKey, true);
}
else
{
// Copy the dictionary so that we can guarantee that routeValues uses an OrdinalIgnoreCase comparer
// and to add the extra "httproute" key used by all Web API routes to disambiguate them from other MVC routes.
routeValues = new HttpRouteValueDictionary(routeValues);
if (!routeValues.ContainsKey(HttpRoute.HttpRouteKey))
{
routeValues.Add(HttpRoute.HttpRouteKey, true);
}
}
HttpConfiguration configuration = request.GetConfiguration();
if (configuration == null)
{
throw Error.InvalidOperation(SRResources.HttpRequestMessageExtensions_NoConfiguration);
}
IHttpVirtualPathData vpd = configuration.Routes.GetVirtualPath(
request: request,
name: routeName,
values: routeValues);
if (vpd == null)
{
return null;
}
return vpd.VirtualPath;
}
}
}