public void CompoundRouteConstraintTests(bool inner1Result, bool inner2Result, bool expected)
        {
            // Arrange
            var inner1 = MockConstraintWithResult(inner1Result);

            var inner2 = MockConstraintWithResult(inner2Result);

            // Act
            var constraint = new CompoundRouteConstraint(new[] { inner1.Object, inner2.Object });
            var actual     = TestValue(constraint, null);

            // Assert
            Assert.Equal(expected, actual);
        }
        public void ParseRouteTemplate_ConstraintsDefaultsAndOptionalsInMultipleSections()
        {
            var result = Act(@"some/url-{p1:alpha:length(3)=hello}/{p2=abc}/{p3?}");

            Assert.Equal("some/url-{p1}/{p2}/{p3}", result.RouteUrl);

            Assert.Equal("hello", result.Defaults["p1"]);
            Assert.Equal("abc", result.Defaults["p2"]);
            Assert.Equal(OptionalParameter, result.Defaults["p3"]);

            CompoundRouteConstraint constraint = Assert.IsType <CompoundRouteConstraint>(result.Constraints["p1"]);

            Assert.IsType <AlphaRouteConstraint>(constraint.Constraints.ElementAt(0));
            Assert.IsType <LengthRouteConstraint>(constraint.Constraints.ElementAt(1));
        }
        public void ParseRouteTemplate_ChainedConstraints()
        {
            var result = Act(@"hello/{param:regex(\d+):regex(\w+)}");

            Assert.Equal("hello/{param}", result.RouteUrl);

            CompoundRouteConstraint constraint = Assert.IsType <CompoundRouteConstraint>(
                result.Constraints["param"]
                );

            Assert.Equal(
                @"\d+",
                Assert.IsType <RegexRouteConstraint>(constraint.Constraints.ElementAt(0)).Pattern
                );
            Assert.Equal(
                @"\w+",
                Assert.IsType <RegexRouteConstraint>(constraint.Constraints.ElementAt(1)).Pattern
                );
        }
Example #4
0
        public static void RegisterRoutes(RouteCollection routes)
        {
            var defaultNamespace   = "ASP.NET.Day1.Controllers";
            var compoundConstraint = new CompoundRouteConstraint(new IRouteConstraint[] { new MinLengthRouteConstraint(3), new AlphaRouteConstraint() });

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Json",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index" },
                constraints: new { id = new RangeRouteConstraint(1, 10) },
                namespaces: new [] { "JsonControllers" }
                );

            routes.MapRoute(
                name: "View",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index" },
                constraints: new { id = new RangeRouteConstraint(11, 100) },
                namespaces: new [] { "Controllers" }
                );

            routes.MapRoute(
                name: "CustomSection1",
                url: "{controller}/{action}/{type}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { type = "json" },
                namespaces: new[] { "JsonControllers" }
                );

            routes.MapRoute(
                name: "CustomSection2",
                url: "{controller}/{action}/{type}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { type = "view" },
                namespaces: new[] { "Controllers" }
                );

            routes.MapRoute(
                name: "Compound",
                url: "{controller}/{action}/{name}/{lastName}",
                defaults: new { controller = "Hello", action = "Index" },
                constraints: new { name = compoundConstraint, lastName = compoundConstraint },
                namespaces: new[] { defaultNamespace }
                );

            routes.MapRoute(
                name: "CustomConstraint",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                constraints: new { language = new LanguageConstraint("ru") },
                namespaces: new[] { "JsonControllers" }
                );

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new [] { defaultNamespace }
                );

            routes.MapRoute(
                name: "Static",
                url: "Home/Index",
                defaults: new { controller = "Home", action = "Index" },
                namespaces: new [] { defaultNamespace }
                );
        }