2009-05-12 Marek Habersack <mhabersack@novell.com>
authorMarek Habersack <grendel@twistedcode.net>
Tue, 12 May 2009 20:37:34 +0000 (20:37 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Tue, 12 May 2009 20:37:34 +0000 (20:37 -0000)
* UrlPattern.cs: TrySubstitute performs substitution trimming. If
a segment would be set to a default value and all of its following
segments as well, it will be omitted from the generated URL.

svn path=/trunk/mcs/; revision=133990

mcs/class/System.Web.Routing/System.Web.Routing/ChangeLog
mcs/class/System.Web.Routing/System.Web.Routing/UrlPattern.cs

index 7eec2766b26328495734f03115d1c4fe3cd7c87e..f145bb1e3c5e590cbc80178e1f535cac6fcfdb7b 100644 (file)
@@ -1,3 +1,9 @@
+2009-05-12  Marek Habersack  <mhabersack@novell.com>
+
+       * UrlPattern.cs: TrySubstitute performs substitution trimming. If
+       a segment would be set to a default value and all of its following
+       segments as well, it will be omitted from the generated URL.
+
 2009-05-11  Marek Habersack  <mhabersack@novell.com>
 
        * UrlPattern.cs: TrySubstitute treats defaults differently
index 6b23a0e264c1b5e60f22954b6f9b0f8dd2aad05f..0a926138adf26e466c4b98a4735f48551a825aa3 100644 (file)
@@ -215,19 +215,47 @@ namespace System.Web.Routing
                        if (values == null) {
                                value = Url;
                                return true;
-                       } else {
-                               foreach (string token in tokens) {
-                                       if (!values.ContainsKey (token)) {
-                                               value = null;
-                                               return false;
-                                       }
+                       }
+
+                       var trim = new Dictionary <string, bool> ();
+                       bool trimValue;
+
+                       foreach (string token in tokens) {
+                               if (!values.ContainsKey (token)) {
+                                       value = null;
+                                       return false;
                                }
+
+                               object left, right;
+                               if (values.TryGetValue (token, out left) && defaults != null && defaults.TryGetValue (token, out right)) {
+                                       if (left == right)
+                                               trimValue = true;
+                                       else
+                                               trimValue = false;
+                               } else
+                                       trimValue = false;
+
+                               if (!trimValue) {
+                                       var keys = trim.Keys;
+                                       int count = keys.Count;
+                                       var keynames = new List <string> (count);
+                                       int i = 0;
+                                       
+                                       foreach (string k in keys)
+                                               keynames.Add (k);
+
+                                       foreach (string k in keynames)
+                                               trim [k] = false;
+                               }
+                               
+                               if (!trim.ContainsKey (token))
+                                       trim.Add (token, trimValue);
                        }
 
                        var replacements = new RouteValueDictionary (values);
                        if (defaults != null) {
                                string key;
-                       
+                               
                                foreach (var de in defaults) {
                                        key = de.Key;
                                        if (replacements.ContainsKey (key))
@@ -240,8 +268,22 @@ namespace System.Web.Routing
                        string [] arr = Url.Split (substsep, StringSplitOptions.None);
                        for (int i = 0; i < arr.Length; i++) {
                                string s = arr [i];
-                               foreach (var p in replacements)
-                                       s = s.Replace ("{" + p.Key + "}", p.Value.ToString ());
+                               foreach (var p in replacements) {
+                                       string pKey = p.Key;
+                                       string pValue;
+                                       bool doTrim;
+                                       
+                                       if (trim.TryGetValue (pKey, out doTrim)) {
+                                               if (doTrim)
+                                                       pValue = String.Empty;
+                                               else
+                                                       pValue = p.Value.ToString ();
+                                       } else
+                                               pValue = p.Value.ToString ();
+
+                                       s = s.Replace ("{" + pKey + "}", pValue);
+                               }
+                               
                                arr [i] = s;
                        }
                        value = String.Join ("{{", arr);