1 // Copyright 2004-2007 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 using Castle
.Core
.Logging
;
17 namespace Castle
.MonoRail
.Framework
.Routing
20 using System
.Collections
.Generic
;
23 using Castle
.MonoRail
.Framework
.Services
.Utils
;
24 using Castle
.MonoRail
.Framework
.Internal
;
29 public class RoutingModuleEx
: IHttpModule
31 private static readonly RoutingEngine engine
= new RoutingEngine();
34 /// Inits the specified app.
36 /// <param name="app">The app.</param>
37 public void Init(HttpApplication app
)
39 app
.BeginRequest
+= OnBeginRequest
;
43 /// Disposes of the resources (other than memory)
44 /// used by the module that implements <see cref="T:System.Web.IHttpModule"/>.
51 /// Called when [begin request].
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
);
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)
76 paramsAsQueryString
+= url
.Substring(queryStringIndex
+ 1);
80 paramsAsQueryString
= url
.Substring(queryStringIndex
+ 1);
84 if (paramsAsQueryString
.Length
!= 0)
86 context
.RewritePath(mrPath
, request
.PathInfo
, paramsAsQueryString
);
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
;
117 /// <value>The engine.</value>
118 public static RoutingEngine Engine
120 get { return engine; }