1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Services
18 using System
.Collections
;
22 #region UrlBuilderParameters
25 /// Represents an static typed definition of the
26 /// <see cref="IUrlBuilder"/> parameters.
28 public class UrlBuilderParameters
30 private string area
, controller
, action
;
31 private string domain
, subdomain
, protocol
, basePath
, pathInfo
;
32 private string routeName
;
34 private object routeParameters
;
35 private object queryString
;
36 private IDictionary customParameters
;
37 private bool encodeForLink
, createAbsolutePath
, useCurrentRouteParams
;
38 private RouteMatch routeMatch
;
41 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
43 public UrlBuilderParameters()
48 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
50 public UrlBuilderParameters(string area
, string controller
, string action
)
53 this.controller
= controller
;
58 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
60 /// <param name="controller">The controller.</param>
61 /// <param name="action">The action.</param>
62 public UrlBuilderParameters(string controller
, string action
)
64 this.controller
= controller
;
69 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
71 /// <param name="controller">The controller.</param>
72 /// <param name="action">The action.</param>
73 /// <param name="queryString">The query string.</param>
74 public UrlBuilderParameters(string controller
, string action
, object queryString
)
76 this.controller
= controller
;
78 this.queryString
= queryString
;
82 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
84 private UrlBuilderParameters(string area
, string controller
, string action
,
85 bool createAbsolutePath
, string basePath
, string domain
, string subdomain
, string protocol
, int port
,
86 string pathInfo
, object queryString
, bool encodeForLink
, IDictionary customParameters
,
87 object routeParams
, string routeName
) :
88 this(area
, controller
, action
)
90 this.createAbsolutePath
= createAbsolutePath
;
91 this.basePath
= basePath
;
93 this.subdomain
= subdomain
;
94 this.protocol
= protocol
;
96 this.pathInfo
= pathInfo
;
97 this.queryString
= queryString
;
98 this.encodeForLink
= encodeForLink
;
99 this.customParameters
= customParameters
;
101 routeParameters
= routeParams
;
102 this.routeName
= routeName
;
106 /// Creates an <see cref="UrlBuilderParameters"/> from the
109 /// <param name="parameters">The parameters.</param>
110 /// <returns></returns>
111 public static UrlBuilderParameters
From(IDictionary parameters
)
113 object routeParams
= CommonUtils
.ObtainObjectEntryAndRemove(parameters
, "params");
115 if (routeParams
== null)
117 routeParams
= CommonUtils
.ObtainObjectEntryAndRemove(parameters
, "routeparams");
120 return From(parameters
, routeParams
);
124 /// Creates an <see cref="UrlBuilderParameters"/> from the
127 /// <param name="parameters">The parameters.</param>
128 /// <param name="routeParams">The route params.</param>
129 /// <returns></returns>
130 public static UrlBuilderParameters
From(IDictionary parameters
, object routeParams
)
132 bool encodeForLink
= CommonUtils
.ObtainEntryAndRemove(parameters
, "encode", "false") == "true";
133 bool useCurrentRouteParams
= CommonUtils
.ObtainEntryAndRemove(parameters
, "useCurrentRouteParams", "false") == "true";
135 string area
= CommonUtils
.ObtainEntryAndRemove(parameters
, "area");
136 string controller
= CommonUtils
.ObtainEntryAndRemove(parameters
, "controller");
137 string action
= CommonUtils
.ObtainEntryAndRemove(parameters
, "action");
139 bool createAbsolutePath
= CommonUtils
.ObtainEntryAndRemove(parameters
, "absolute", "false") == "true";
140 string basePath
= CommonUtils
.ObtainEntryAndRemove(parameters
, "basePath");
141 string domain
= CommonUtils
.ObtainEntryAndRemove(parameters
, "domain");
142 string subdomain
= CommonUtils
.ObtainEntryAndRemove(parameters
, "subdomain");
143 string protocol
= CommonUtils
.ObtainEntryAndRemove(parameters
, "protocol");
144 int port
= Convert
.ToInt32(CommonUtils
.ObtainEntryAndRemove(parameters
, "port", "0"));
146 string routeName
= CommonUtils
.ObtainEntryAndRemove(parameters
, "route");
148 string pathInfo
= CommonUtils
.ObtainEntryAndRemove(parameters
, "pathinfo");
149 object queryString
= CommonUtils
.ObtainObjectEntryAndRemove(parameters
, "querystring");
150 RouteMatch routeMatch
= (RouteMatch
) CommonUtils
.ObtainObjectEntryAndRemove(parameters
, "routeMatch");
152 return new UrlBuilderParameters(area
, controller
, action
,
153 createAbsolutePath
, basePath
, domain
, subdomain
, protocol
, port
,
154 pathInfo
, queryString
,
155 encodeForLink
, parameters
, routeParams
, routeName
).
156 SetRouteMatch(useCurrentRouteParams
, routeMatch
);
160 /// Sets the query string.
162 /// <param name="queryString">The query string.</param>
163 /// <returns></returns>
164 public UrlBuilderParameters
SetQueryString(object queryString
)
166 this.queryString
= queryString
;
171 /// Sets the route match.
173 /// <param name="useCurrentRouteParams">if set to <c>true</c> [use current route params].</param>
174 /// <param name="routeMatch">The route match.</param>
175 /// <returns></returns>
176 public UrlBuilderParameters
SetRouteMatch(bool useCurrentRouteParams
, RouteMatch routeMatch
)
178 this.useCurrentRouteParams
= useCurrentRouteParams
;
179 this.routeMatch
= routeMatch
;
184 /// Sets the route match.
186 /// <param name="routeMatch">The route match.</param>
187 /// <returns></returns>
188 public UrlBuilderParameters
SetRouteMatch(RouteMatch routeMatch
)
190 this.routeMatch
= routeMatch
;
195 /// Gets or sets a value indicating whether [create absolute path].
197 /// <value><c>true</c> if [create absolute path]; otherwise, <c>false</c>.</value>
198 public bool CreateAbsolutePath
200 get { return createAbsolutePath; }
201 set { createAbsolutePath = value; }
205 /// Gets or sets a value indicating whether [encode for link].
207 /// <value><c>true</c> if [encode for link]; otherwise, <c>false</c>.</value>
208 public bool EncodeForLink
210 get { return encodeForLink; }
211 set { encodeForLink = value; }
215 /// Gets or sets the area.
217 /// <value>The area.</value>
221 set { area = value; }
225 /// Gets or sets the controller.
227 /// <value>The controller.</value>
228 public string Controller
230 get { return controller; }
231 set { controller = value; }
235 /// Gets or sets the action.
237 /// <value>The action.</value>
240 get { return action; }
241 set { action = value; }
245 /// Gets or sets the domain.
247 /// <value>The domain.</value>
250 get { return domain; }
251 set { domain = value; }
255 /// Gets or sets the subdomain.
257 /// <value>The subdomain.</value>
258 public string Subdomain
260 get { return subdomain; }
261 set { subdomain = value; }
265 /// Gets or sets the protocol.
267 /// <value>The protocol.</value>
268 public string Protocol
270 get { return protocol; }
271 set { protocol = value; }
275 /// Gets or sets the base path.
277 /// <value>The base path.</value>
278 public string BasePath
280 get { return basePath; }
281 set { basePath = value; }
285 /// Gets or sets the port.
287 /// <value>The port.</value>
291 set { port = value; }
295 /// Gets or sets the path info.
297 /// <value>The path info.</value>
298 public string PathInfo
300 get { return pathInfo; }
301 set { pathInfo = value; }
305 /// Gets or sets the query string.
307 /// <value>The query string.</value>
308 public object QueryString
310 get { return queryString; }
311 set { queryString = value; }
315 /// Gets or sets a value indicating whether [use current route params].
318 /// <c>true</c> if [use current route params]; otherwise, <c>false</c>.
320 public bool UseCurrentRouteParams
322 get { return useCurrentRouteParams; }
323 set { useCurrentRouteParams = value; }
327 /// Gets the name of the route.
329 /// <value>The name of the route.</value>
330 public string RouteName
332 get { return routeName; }
333 set { routeName = value; }
337 /// Gets or sets the route parameters.
339 /// <value>The route parameters.</value>
340 public object RouteParameters
342 get { return routeParameters; }
343 set { routeParameters = value; }
347 /// Gets or sets the route match.
349 /// <value>The route match.</value>
350 public RouteMatch RouteMatch
352 get { return routeMatch; }
353 set { routeMatch = value; }
357 /// Gets or sets the custom parameters.
359 /// <value>The custom parameters.</value>
360 public IDictionary CustomParameters
362 get { return customParameters; }
363 set { customParameters = value; }
370 /// THe UrlBuilder service centralizes the url generation used by the whole
371 /// framework including redirect urls, urls generated by helpers and so on.
372 /// It offers a central place to change MonoRail behavior on how to deal with urls.
374 public interface IUrlBuilder
377 /// Gets or sets a value indicating whether the builder should output an extension.
379 /// <value><c>true</c> if should use extensions; otherwise, <c>false</c>.</value>
380 bool UseExtensions { get; set; }
383 /// Builds the URL using the current url as contextual information and the specified parameters.
385 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
386 /// for more information regarding the parameters.
389 /// <param name="current">The current Url information.</param>
390 /// <param name="parameters">The parameters.</param>
391 /// <returns></returns>
392 string BuildUrl(UrlInfo current
, UrlBuilderParameters parameters
);
395 /// Builds the URL using the current url as contextual information and the specified parameters.
397 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
398 /// for more information regarding the parameters.
401 /// <param name="current">The current Url information.</param>
402 /// <param name="parameters">The parameters.</param>
403 /// <param name="routeParameters">The route parameters.</param>
404 /// <returns></returns>
405 string BuildUrl(UrlInfo current
, UrlBuilderParameters parameters
, IDictionary routeParameters
);
408 /// Builds the URL using the current url as contextual information and the specified parameters.
410 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
411 /// for more information regarding the parameters.
414 /// <param name="current">The current Url information.</param>
415 /// <param name="parameters">The parameters.</param>
416 /// <returns></returns>
417 string BuildUrl(UrlInfo current
, IDictionary parameters
);
420 /// Builds the URL using the current url as contextual information and the specified parameters.
422 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
423 /// for more information regarding the parameters.
426 /// <param name="current">The current Url information.</param>
427 /// <param name="parameters">The parameters.</param>
428 /// <param name="routeParameters">The route parameters.</param>
429 /// <returns></returns>
430 string BuildUrl(UrlInfo current
, IDictionary parameters
, IDictionary routeParameters
);
433 /// Builds the URL using the current url as contextual information and the specified parameters.
435 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
436 /// for more information regarding the parameters.
439 /// <param name="current">The current Url information.</param>
440 /// <param name="parameters">The parameters.</param>
441 /// <returns></returns>
442 UrlParts
CreateUrlPartsBuilder(UrlInfo current
, UrlBuilderParameters parameters
);
445 /// Builds the URL using the current url as contextual information and the specified parameters.
447 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
448 /// for more information regarding the parameters.
451 /// <param name="current">The current Url information.</param>
452 /// <param name="parameters">The parameters.</param>
453 /// <param name="routeParameters">The route parameters.</param>
454 /// <returns></returns>
455 UrlParts
CreateUrlPartsBuilder(UrlInfo current
, UrlBuilderParameters parameters
, IDictionary routeParameters
);
458 /// Builds the URL using the current url as contextual information and the specified parameters.
460 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
461 /// for more information regarding the parameters.
464 /// <param name="current">The current Url information.</param>
465 /// <param name="parameters">The parameters.</param>
466 /// <returns></returns>
467 UrlParts
CreateUrlPartsBuilder(UrlInfo current
, IDictionary parameters
);
470 /// Builds the URL using the current url as contextual information and the specified parameters.
472 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
473 /// for more information regarding the parameters.
476 /// <param name="current">The current Url information.</param>
477 /// <param name="parameters">The parameters.</param>
478 /// <param name="routeParameters">The route parameters.</param>
479 /// <returns></returns>
480 UrlParts
CreateUrlPartsBuilder(UrlInfo current
, IDictionary parameters
, IDictionary routeParameters
);