Exemple #1
0
        public void GetMatches_LinkToPageFromController_WithActionValueAmbiguous()
        {
            // Arrange
            var entries = new List <OutboundMatch>();

            var entry1 = CreateMatch(new { controller = "Home", action = "Index", area = (string)null, page = (string)null, });

            entry1.Entry.RouteTemplate = TemplateParser.Parse("a");
            entries.Add(entry1);

            var entry2 = CreateMatch(new { page = "/Store/Buy", area = (string)null, controller = (string)null, action = (string)null, });

            entry2.Entry.RouteTemplate = TemplateParser.Parse("b");
            entries.Add(entry2);

            var tree = new LinkGenerationDecisionTree(entries);

            var context = CreateContext(new { page = "/Store/Buy", action = "Index", }, new { controller = "Home", action = "Index", page = "16", });

            // Act
            var matches = tree.GetMatches(context.Values, context.AmbientValues).Select(m => m.Match).ToList();

            // Assert
            Assert.Empty(matches);
        }
        /// <inheritdoc />
        public VirtualPathData GetVirtualPath(VirtualPathContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // If it's a named route we will try to generate a link directly and
            // if we can't, we will not try to generate it using an unnamed route.
            if (context.RouteName != null)
            {
                return(GetVirtualPathForNamedRoute(context));
            }

            // The decision tree will give us back all entries that match the provided route data in the correct
            // order. We just need to iterate them and use the first one that can generate a link.
            var matches = _linkGenerationTree.GetMatches(context);

            if (matches == null)
            {
                return(null);
            }

            for (var i = 0; i < matches.Count; i++)
            {
                var path = GenerateVirtualPath(context, matches[i].Match.Entry, matches[i].Match.TemplateBinder);
                if (path != null)
                {
                    return(path);
                }
            }

            return(null);
        }
Exemple #3
0
        public void GetMatches_PagesWithArea_AllValuesAmbient()
        {
            // Arrange
            var entries = new List <OutboundMatch>();

            var entry1 = CreateMatch(new { page = "/Store/Buy", area = (string)null, });

            entry1.Entry.RouteTemplate = TemplateParser.Parse("a");
            entries.Add(entry1);

            var entry2 = CreateMatch(new { page = "/Store/Buy", area = "Admin" });

            entry2.Entry.RouteTemplate = TemplateParser.Parse("b");
            entries.Add(entry2);

            var tree = new LinkGenerationDecisionTree(entries);

            var context = CreateContext(new { }, new { page = "/Store/Buy", area = "Admin", });

            // Act
            var matches = tree.GetMatches(context.Values, context.AmbientValues).Select(m => m.Match).ToList();

            // Assert
            Assert.Collection(
                matches,
                m => { Assert.Same(entry2, m); },
                m => { Assert.Same(entry1, m); });
        }
Exemple #4
0
        public void SelectMultipleEntries_BothMatch_OrderedByTemplate()
        {
            // Arrange
            var entries = new List <OutboundMatch>();

            var entry1 = CreateMatch(new { controller = "Store", action = "Buy" });

            entry1.Entry.RouteTemplate = TemplateParser.Parse("a");
            entries.Add(entry1);

            var entry2 = CreateMatch(new { controller = "Store", action = "Buy" });

            entry2.Entry.RouteTemplate = TemplateParser.Parse("b");
            entries.Add(entry2);

            var tree = new LinkGenerationDecisionTree(entries);

            var context = CreateContext(new { controller = "Store", action = "Buy" });

            // Act
            var matches = tree.GetMatches(context.Values, context.AmbientValues).Select(m => m.Match).ToList();

            // Assert
            Assert.Equal(entries, matches);
        }
Exemple #5
0
        public void SelectMultipleEntries_BothMatch_CriteriaSubset()
        {
            // Arrange
            var entries = new List <OutboundMatch>();

            var entry1 = CreateMatch(new { controller = "Store", action = "Buy" });

            entries.Add(entry1);

            var entry2 = CreateMatch(new { controller = "Store" });

            entry2.Entry.Order = 1;
            entries.Add(entry2);

            var tree = new LinkGenerationDecisionTree(entries);

            var context = CreateContext(
                values: new { controller = "Store" },
                ambientValues: new { controller = "Store", action = "Buy" });

            // Act
            var matches = tree.GetMatches(context.Values, context.AmbientValues).Select(m => m.Match).ToList();

            // Assert
            Assert.Equal(entries, matches);
        }
Exemple #6
0
        public void SelectMultipleEntries_OneDoesntMatch()
        {
            // Arrange
            var entries = new List <OutboundMatch>();

            var entry1 = CreateMatch(new { controller = "Store", action = "Buy" });

            entries.Add(entry1);

            var entry2 = CreateMatch(new { controller = "Store", action = "Cart" });

            entries.Add(entry2);

            var tree = new LinkGenerationDecisionTree(entries);

            var context = CreateContext(
                values: new { controller = "Store" },
                ambientValues: new { controller = "Store", action = "Buy" });

            // Act
            var matches = tree.GetMatches(context.Values, context.AmbientValues);

            // Assert
            Assert.Same(entry1, Assert.Single(matches).Match);
        }
Exemple #7
0
        public void SelectSingleEntry_MultipleCriteria()
        {
            // Arrange
            var entries = new List <OutboundMatch>();

            var entry = CreateMatch(new { controller = "Store", action = "Buy" });

            entries.Add(entry);

            var tree = new LinkGenerationDecisionTree(entries);

            var context = CreateContext(new { controller = "Store", action = "Buy" });

            // Act
            var matches = tree.GetMatches(context.Values, context.AmbientValues);

            // Assert
            Assert.Same(entry, Assert.Single(matches).Match);
        }
Exemple #8
0
        public void GetMatches_AllowsNullAmbientValues()
        {
            // Arrange
            var entries = new List <OutboundMatch>();

            var entry = CreateMatch(new { });

            entries.Add(entry);

            var tree = new LinkGenerationDecisionTree(entries);

            var context = CreateContext(new { });

            // Act
            var matches = tree.GetMatches(context.Values, ambientValues: null);

            // Assert
            Assert.Same(entry, Assert.Single(matches).Match);
        }
Exemple #9
0
        public void SelectSingleEntry_MultipleCriteria_AmbientValue_Ignored()
        {
            // Arrange
            var entries = new List <OutboundMatch>();

            var entry = CreateMatch(new { controller = "Store", action = (string)null });

            entries.Add(entry);

            var tree = new LinkGenerationDecisionTree(entries);

            var context = CreateContext(
                values: new { controller = "Store" },
                ambientValues: new { controller = "Store", action = "Buy" });

            // Act
            var matches = tree.GetMatches(context.Values, context.AmbientValues);

            // Assert
            var match = Assert.Single(matches);

            Assert.Same(entry, match.Match);
            Assert.True(match.IsFallbackMatch);
        }