RouteUrlExpressionBuilder.cs 4.3 KB

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