Added RedirectUsingNamedRoute
[castle.git] / MonoRail / Castle.MonoRail.Framework / Services / IUrlBuilder.cs
blob17094a7a515f090efd443cd4f35c71c4c9d5ce2c
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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
17 using System;
18 using System.Collections;
19 using Internal;
20 using Routing;
22 #region UrlBuilderParameters
24 /// <summary>
25 /// Represents an static typed definition of the
26 /// <see cref="IUrlBuilder"/> parameters.
27 /// </summary>
28 public class UrlBuilderParameters
30 private string area, controller, action;
31 private string domain, subdomain, protocol, basePath, pathInfo;
32 private string routeName;
33 private int port;
34 private object routeParameters;
35 private object queryString;
36 private IDictionary customParameters;
37 private bool encodeForLink, createAbsolutePath, useCurrentRouteParams;
38 private RouteMatch routeMatch;
40 /// <summary>
41 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
42 /// </summary>
43 public UrlBuilderParameters()
47 /// <summary>
48 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
49 /// </summary>
50 public UrlBuilderParameters(string area, string controller, string action)
52 this.area = area;
53 this.controller = controller;
54 this.action = action;
57 /// <summary>
58 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
59 /// </summary>
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;
65 this.action = action;
68 /// <summary>
69 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
70 /// </summary>
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;
77 this.action = action;
78 this.queryString = queryString;
81 /// <summary>
82 /// Initializes a new instance of the <see cref="UrlBuilderParameters"/> class.
83 /// </summary>
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;
92 this.domain = domain;
93 this.subdomain = subdomain;
94 this.protocol = protocol;
95 this.port = port;
96 this.pathInfo = pathInfo;
97 this.queryString = queryString;
98 this.encodeForLink = encodeForLink;
99 this.customParameters = customParameters;
101 routeParameters = routeParams;
102 this.routeName = routeName;
105 /// <summary>
106 /// Creates an <see cref="UrlBuilderParameters"/> from the
107 /// dictionary
108 /// </summary>
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);
123 /// <summary>
124 /// Creates an <see cref="UrlBuilderParameters"/> from the
125 /// dictionary
126 /// </summary>
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);
159 /// <summary>
160 /// Sets the query string.
161 /// </summary>
162 /// <param name="queryString">The query string.</param>
163 /// <returns></returns>
164 public UrlBuilderParameters SetQueryString(object queryString)
166 this.queryString = queryString;
167 return this;
170 /// <summary>
171 /// Sets the route match.
172 /// </summary>
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;
180 return this;
183 /// <summary>
184 /// Sets the route match.
185 /// </summary>
186 /// <param name="routeMatch">The route match.</param>
187 /// <returns></returns>
188 public UrlBuilderParameters SetRouteMatch(RouteMatch routeMatch)
190 this.routeMatch = routeMatch;
191 return this;
194 /// <summary>
195 /// Gets or sets a value indicating whether [create absolute path].
196 /// </summary>
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; }
204 /// <summary>
205 /// Gets or sets a value indicating whether [encode for link].
206 /// </summary>
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; }
214 /// <summary>
215 /// Gets or sets the area.
216 /// </summary>
217 /// <value>The area.</value>
218 public string Area
220 get { return area; }
221 set { area = value; }
224 /// <summary>
225 /// Gets or sets the controller.
226 /// </summary>
227 /// <value>The controller.</value>
228 public string Controller
230 get { return controller; }
231 set { controller = value; }
234 /// <summary>
235 /// Gets or sets the action.
236 /// </summary>
237 /// <value>The action.</value>
238 public string Action
240 get { return action; }
241 set { action = value; }
244 /// <summary>
245 /// Gets or sets the domain.
246 /// </summary>
247 /// <value>The domain.</value>
248 public string Domain
250 get { return domain; }
251 set { domain = value; }
254 /// <summary>
255 /// Gets or sets the subdomain.
256 /// </summary>
257 /// <value>The subdomain.</value>
258 public string Subdomain
260 get { return subdomain; }
261 set { subdomain = value; }
264 /// <summary>
265 /// Gets or sets the protocol.
266 /// </summary>
267 /// <value>The protocol.</value>
268 public string Protocol
270 get { return protocol; }
271 set { protocol = value; }
274 /// <summary>
275 /// Gets or sets the base path.
276 /// </summary>
277 /// <value>The base path.</value>
278 public string BasePath
280 get { return basePath; }
281 set { basePath = value; }
284 /// <summary>
285 /// Gets or sets the port.
286 /// </summary>
287 /// <value>The port.</value>
288 public int Port
290 get { return port; }
291 set { port = value; }
294 /// <summary>
295 /// Gets or sets the path info.
296 /// </summary>
297 /// <value>The path info.</value>
298 public string PathInfo
300 get { return pathInfo; }
301 set { pathInfo = value; }
304 /// <summary>
305 /// Gets or sets the query string.
306 /// </summary>
307 /// <value>The query string.</value>
308 public object QueryString
310 get { return queryString; }
311 set { queryString = value; }
314 /// <summary>
315 /// Gets or sets a value indicating whether [use current route params].
316 /// </summary>
317 /// <value>
318 /// <c>true</c> if [use current route params]; otherwise, <c>false</c>.
319 /// </value>
320 public bool UseCurrentRouteParams
322 get { return useCurrentRouteParams; }
323 set { useCurrentRouteParams = value; }
326 /// <summary>
327 /// Gets the name of the route.
328 /// </summary>
329 /// <value>The name of the route.</value>
330 public string RouteName
332 get { return routeName; }
333 set { routeName = value; }
336 /// <summary>
337 /// Gets or sets the route parameters.
338 /// </summary>
339 /// <value>The route parameters.</value>
340 public object RouteParameters
342 get { return routeParameters; }
343 set { routeParameters = value; }
346 /// <summary>
347 /// Gets or sets the route match.
348 /// </summary>
349 /// <value>The route match.</value>
350 public RouteMatch RouteMatch
352 get { return routeMatch; }
353 set { routeMatch = value; }
356 /// <summary>
357 /// Gets or sets the custom parameters.
358 /// </summary>
359 /// <value>The custom parameters.</value>
360 public IDictionary CustomParameters
362 get { return customParameters; }
363 set { customParameters = value; }
367 #endregion
369 /// <summary>
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.
373 /// </summary>
374 public interface IUrlBuilder
376 /// <summary>
377 /// Gets or sets a value indicating whether the builder should output an extension.
378 /// </summary>
379 /// <value><c>true</c> if should use extensions; otherwise, <c>false</c>.</value>
380 bool UseExtensions { get; set; }
382 /// <summary>
383 /// Builds the URL using the current url as contextual information and the specified parameters.
384 /// <para>
385 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
386 /// for more information regarding the parameters.
387 /// </para>
388 /// </summary>
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);
394 /// <summary>
395 /// Builds the URL using the current url as contextual information and the specified parameters.
396 /// <para>
397 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
398 /// for more information regarding the parameters.
399 /// </para>
400 /// </summary>
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);
407 /// <summary>
408 /// Builds the URL using the current url as contextual information and the specified parameters.
409 /// <para>
410 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
411 /// for more information regarding the parameters.
412 /// </para>
413 /// </summary>
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);
419 /// <summary>
420 /// Builds the URL using the current url as contextual information and the specified parameters.
421 /// <para>
422 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
423 /// for more information regarding the parameters.
424 /// </para>
425 /// </summary>
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);
432 /// <summary>
433 /// Builds the URL using the current url as contextual information and the specified parameters.
434 /// <para>
435 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
436 /// for more information regarding the parameters.
437 /// </para>
438 /// </summary>
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);
444 /// <summary>
445 /// Builds the URL using the current url as contextual information and the specified parameters.
446 /// <para>
447 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
448 /// for more information regarding the parameters.
449 /// </para>
450 /// </summary>
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);
457 /// <summary>
458 /// Builds the URL using the current url as contextual information and the specified parameters.
459 /// <para>
460 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
461 /// for more information regarding the parameters.
462 /// </para>
463 /// </summary>
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);
469 /// <summary>
470 /// Builds the URL using the current url as contextual information and the specified parameters.
471 /// <para>
472 /// Common parameters includes area, controller and action. See <see cref="UrlBuilderParameters"/>
473 /// for more information regarding the parameters.
474 /// </para>
475 /// </summary>
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);