Fix the build.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Routing / RoutingModuleEx.cs
blobacde55d795acf5f5dc7c173998a8dfd82abc8583
1 // Copyright 2004-2007 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 using Castle.Core.Logging;
17 namespace Castle.MonoRail.Framework.Routing
19 using System;
20 using System.Collections.Generic;
21 using System.Text;
22 using System.Web;
23 using Castle.MonoRail.Framework.Services.Utils;
24 using Castle.MonoRail.Framework.Internal;
26 /// <summary>
27 /// Pendent
28 /// </summary>
29 public class RoutingModuleEx : IHttpModule
31 private static readonly RoutingEngine engine = new RoutingEngine();
33 /// <summary>
34 /// Inits the specified app.
35 /// </summary>
36 /// <param name="app">The app.</param>
37 public void Init(HttpApplication app)
39 app.BeginRequest += OnBeginRequest;
42 /// <summary>
43 /// Disposes of the resources (other than memory)
44 /// used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
45 /// </summary>
46 public void Dispose()
50 /// <summary>
51 /// Called when [begin request].
52 /// </summary>
53 /// <param name="sender">The sender.</param>
54 /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
55 public void OnBeginRequest(object sender, EventArgs e)
57 HttpContext context = HttpContext.Current;
58 HttpRequest request = context.Request;
60 RouteMatch match = engine.FindMatch(request.Headers["host"], request.ApplicationPath, request.RawUrl);
62 if (match != null)
64 string mrPath = CreateMrPath(match);
65 string url = request.RawUrl;
67 string paramsAsQueryString = ConvertToQueryString(match.Parameters, context.Server);
69 int queryStringIndex = url.IndexOf('?');
71 if (queryStringIndex != -1)
73 if (paramsAsQueryString.Length != 0)
75 // Concat
76 paramsAsQueryString += url.Substring(queryStringIndex + 1);
78 else
80 paramsAsQueryString = url.Substring(queryStringIndex + 1);
84 if (paramsAsQueryString.Length != 0)
86 context.RewritePath(mrPath, request.PathInfo, paramsAsQueryString);
88 else
90 context.RewritePath(mrPath);
95 private static string ConvertToQueryString(Dictionary<string, string> parameters, HttpServerUtility serverUtil)
97 StringBuilder sb = new StringBuilder();
99 foreach(KeyValuePair<string, string> pair in parameters)
101 sb.AppendFormat("{0}={1}&", serverUtil.UrlEncode(pair.Key), serverUtil.UrlEncode(pair.Value));
104 return sb.ToString();
107 private static string CreateMrPath(RouteMatch match)
109 ControllerDescriptor desc = ControllerInspectionUtil.Inspect(match.ControllerType);
111 return "~/" + (string.IsNullOrEmpty(desc.Area) ? null : desc.Area + "/") + desc.Name + "/" + match.Action + MonoRailServiceContainer.MonoRailExtension;
114 /// <summary>
115 /// Gets the engine.
116 /// </summary>
117 /// <value>The engine.</value>
118 public static RoutingEngine Engine
120 get { return engine; }