Fixed xamarin bug #13708: routing constraints should be trated as 'convertible to...
authorPablo Ruiz Garcia <pablo.ruiz@gmail.com>
Mon, 5 Aug 2013 13:20:31 +0000 (15:20 +0200)
committerPablo Ruiz Garcia <pablo.ruiz@gmail.com>
Mon, 5 Aug 2013 13:20:31 +0000 (15:20 +0200)
mcs/class/System.Web.Routing/System.Web.Routing/Route.cs
mcs/class/System.Web.Routing/Test/System.Web.Routing/RouteTest.cs

index 32b0ba9705a080ee98cf0a91f65e00a1e024543d..aeb02c319b2e0a68a9215c39e016182769fcf419 100644 (file)
@@ -32,6 +32,7 @@ using System.Runtime.CompilerServices;
 using System.Security.Permissions;
 using System.Text.RegularExpressions;
 using System.Web;
+using System.Globalization;
 
 namespace System.Web.Routing
 {
@@ -163,13 +164,15 @@ namespace System.Web.Routing
 
                        string s = constraint as string;
                        if (s != null) {
-                               string v;
+                               string v = null;
                                object o;
 
+                               // NOTE: If constraint was not an IRouteConstraint, is is asumed
+                               // to be an object 'convertible' to string, or at least this is how
+                               // ASP.NET seems to work by the tests i've done latelly. (pruiz)
+
                                if (values != null && values.TryGetValue (parameterName, out o))
-                                       v = o as string;
-                               else
-                                       v = null;
+                                       v = Convert.ToString (o, CultureInfo.InvariantCulture);
 
                                if (!String.IsNullOrEmpty (v))
                                        return MatchConstraintRegex (v, s);
@@ -184,7 +187,7 @@ namespace System.Web.Routing
                                        if (!rdValues.TryGetValue (parameterName, out o))
                                                return false;
 
-                                       v = o as string;
+                                       v = Convert.ToString (o, CultureInfo.InvariantCulture);
                                        if (String.IsNullOrEmpty (v))
                                                return false;
 
index 4ac8cbcdcd95c0b06643ffceedd1dea84eb5b193..e43117793c09901c16a3154e2aacde485204f269 100644 (file)
@@ -1699,6 +1699,30 @@ namespace MonoTests.System.Web.Routing
                        Assert.IsNull(vp, "#3");
                }
 
+               [Test (Description="Xamarin Bug #13708")]
+               public void GetVirtualPath25()
+               {
+                       var r = new MyRoute("{year}/{month}/{slug}", new MyRouteHandler())
+                       {
+                               Defaults = new RouteValueDictionary(new { controller = "Blog", action = "View" }),
+                               Constraints = new RouteValueDictionary(new { year = @"\d{4}", month = @"\d{2}" }),
+                       };
+                       var hc = new HttpContextStub2("~/", String.Empty);
+                       var values = new RouteValueDictionary()
+                       {
+                               { "area", string.Empty },
+                               { "controller", "Blog" },
+                               { "action", "View" },
+                               { "year", 2013 }, // Year as an int, not a string
+                               { "month", "08" },
+                               { "slug", "hello-world" },
+                       };
+                       var vp = r.GetVirtualPath(new RequestContext(hc, new RouteData()), values);
+
+                       Assert.IsNotNull(vp, "#1");
+                       Assert.AreEqual("2013/08/hello-world", vp.VirtualPath, "#2");
+               }
+
                // Bug #500739
                [Test]
                public void RouteGetRequiredStringWithDefaults ()