RouteUrlExpressionBuilder.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. public class RouteUrlExpressionBuilder : ExpressionBuilder
  36. {
  37. static readonly char[] expressionSplitChars = { ',' };
  38. static readonly char[] keyValueSplitChars = { '=' };
  39. public override bool SupportsEvaluate { get { return true; } }
  40. public RouteUrlExpressionBuilder ()
  41. {
  42. }
  43. // This method is used only from within pages that aren't compiled
  44. public override object EvaluateExpression (object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
  45. {
  46. if (entry == null)
  47. throw new NullReferenceException (".NET emulation (entry == null)");
  48. if (context == null)
  49. throw new NullReferenceException (".NET emulation (context == null)");
  50. return GetRouteUrl (context.TemplateControl, entry.Expression);
  51. }
  52. public override CodeExpression GetCodeExpression (BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
  53. {
  54. if (entry == null)
  55. throw new NullReferenceException (".NET emulation (entry == null)");
  56. var ret = new CodeMethodInvokeExpression ();
  57. ret.Method = new CodeMethodReferenceExpression (new CodeTypeReferenceExpression (typeof (RouteUrlExpressionBuilder)), "GetRouteUrl");
  58. CodeExpressionCollection parameters = ret.Parameters;
  59. parameters.Add (new CodeThisReferenceExpression ());
  60. parameters.Add (new CodePrimitiveExpression (entry.Expression));
  61. return ret;
  62. }
  63. public static string GetRouteUrl (Control control, string expression)
  64. {
  65. if (control == null)
  66. throw new ArgumentNullException ("control");
  67. string routeName;
  68. var rvd = new RouteValueDictionary ();
  69. if (!TryParseRouteExpression (expression, rvd, out routeName))
  70. throw new InvalidOperationException ("Invalid expression, RouteUrlExpressionBuilder expects a string with format: RouteName=route,Key1=Value1,Key2=Value2");
  71. return control.GetRouteUrl (routeName, rvd);
  72. }
  73. public static bool TryParseRouteExpression (string expression, RouteValueDictionary routeValues, out string routeName)
  74. {
  75. routeName = null;
  76. if (String.IsNullOrEmpty (expression))
  77. return false;
  78. if (routeValues == null)
  79. throw new NullReferenceException (".NET emulation (routeValues == null)");
  80. string[] parts = expression.Split (expressionSplitChars);
  81. foreach (string part in parts) {
  82. string[] keyval = part.Split (keyValueSplitChars);
  83. if (keyval.Length != 2)
  84. return false;
  85. string key = keyval [0].Trim ();
  86. if (key == String.Empty)
  87. return false;
  88. if (String.Compare (key, "routename", StringComparison.OrdinalIgnoreCase) == 0) {
  89. routeName = keyval [1].Trim ();
  90. continue;
  91. }
  92. if (routeValues.ContainsKey (key))
  93. routeValues [key] = keyval [1].Trim ();
  94. else
  95. routeValues.Add (key, keyval [1].Trim ());
  96. }
  97. return true;
  98. }
  99. }
  100. }