2 // RouteUrlExpressionBuilder.cs
5 // Marek Habersack (mhabersack@novell.com)
7 // (C) 2010 Novell, Inc (http://novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Web.Routing;
36 namespace System.Web.Compilation
38 [ExpressionEditor ("System.Web.UI.Design.RouteUrlExpressionEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
39 [ExpressionPrefix ("Routes")]
40 public class RouteUrlExpressionBuilder : ExpressionBuilder
42 static readonly char[] expressionSplitChars = { ',' };
43 static readonly char[] keyValueSplitChars = { '=' };
45 public override bool SupportsEvaluate { get { return true; } }
47 public RouteUrlExpressionBuilder ()
51 // This method is used only from within pages that aren't compiled
52 public override object EvaluateExpression (object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
55 throw new NullReferenceException (".NET emulation (entry == null)");
58 throw new NullReferenceException (".NET emulation (context == null)");
60 return GetRouteUrl (context.TemplateControl, entry.Expression);
63 public override CodeExpression GetCodeExpression (BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
66 throw new NullReferenceException (".NET emulation (entry == null)");
68 var ret = new CodeMethodInvokeExpression ();
69 ret.Method = new CodeMethodReferenceExpression (new CodeTypeReferenceExpression (typeof (RouteUrlExpressionBuilder)), "GetRouteUrl");
71 CodeExpressionCollection parameters = ret.Parameters;
72 parameters.Add (new CodeThisReferenceExpression ());
73 parameters.Add (new CodePrimitiveExpression (entry.Expression));
78 public static string GetRouteUrl (Control control, string expression)
81 throw new ArgumentNullException ("control");
84 var rvd = new RouteValueDictionary ();
86 if (!TryParseRouteExpression (expression, rvd, out routeName))
87 throw new InvalidOperationException ("Invalid expression, RouteUrlExpressionBuilder expects a string with format: RouteName=route,Key1=Value1,Key2=Value2");
89 return control.GetRouteUrl (routeName, rvd);
92 public static bool TryParseRouteExpression (string expression, RouteValueDictionary routeValues, out string routeName)
95 if (String.IsNullOrEmpty (expression))
98 if (routeValues == null)
99 throw new NullReferenceException (".NET emulation (routeValues == null)");
101 string[] parts = expression.Split (expressionSplitChars);
102 foreach (string part in parts) {
103 string[] keyval = part.Split (keyValueSplitChars);
104 if (keyval.Length != 2)
107 string key = keyval [0].Trim ();
108 if (key == String.Empty)
111 if (String.Compare (key, "routename", StringComparison.OrdinalIgnoreCase) == 0) {
112 routeName = keyval [1].Trim ();
116 if (routeValues.ContainsKey (key))
117 routeValues [key] = keyval [1].Trim ();
119 routeValues.Add (key, keyval [1].Trim ());