コード例 #1
0
        public void OnProvidersExecuting_SkipsPagesStartingWithUnderscore()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var dir1         = fileProvider.AddDirectoryContent("/Pages",
                                                                new[]
            {
                fileProvider.AddFile("/Pages/Home.cshtml", "@page"),
                fileProvider.AddFile("/Pages/_Layout.cshtml", "@page")
            });

            fileProvider.AddDirectoryContent("/", new[] { dir1 });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/";
            var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/Home.cshtml", model.RelativePath);
            });
        }
コード例 #2
0
        public void OnProvidersExecuting_AllowsRouteTemplateWithOverridePattern()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file         = fileProvider.AddFile("/Index.cshtml", "@page \"/custom-route\"");

            fileProvider.AddDirectoryContent("/", new[] { file });

            var fileSystem = new TestRazorProjectFileSystem(fileProvider, _hostingEnvironment);

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/";
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(
                context.RouteModels,
                model =>
            {
                Assert.Equal("/Index.cshtml", model.RelativePath);
                Assert.Equal("/Index", model.ViewEnginePath);
                Assert.Collection(
                    model.Selectors,
                    selector => Assert.Equal("custom-route", selector.AttributeRouteModel.Template));
            });
        }
コード例 #3
0
        public void OnProvidersExecuting_ReturnsPagesWithPageDirective()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file1        = fileProvider.AddFile("/Pages/Home.cshtml", "@page");
            var file2        = fileProvider.AddFile("/Pages/Test.cshtml", "Hello world");

            var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { file1, file2 });

            fileProvider.AddDirectoryContent("/", new[] { dir1 });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/";
            var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/Home.cshtml", model.RelativePath);
                Assert.Equal("/Pages/Home", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Pages/Home", selector.AttributeRouteModel.Template));
            });
        }
コード例 #4
0
        public void OnProvidersExecuting_DoesNotAddPageDirectivesIfItAlreadyExists()
        {
            // Arrange
            var fileSystem = new VirtualRazorProjectFileSystem();

            fileSystem.Add(new TestRazorProjectItem("/Pages/Home.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Pages/Test.cshtml", "@page"));

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/";
            var provider  = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context   = new PageRouteModelProviderContext();
            var pageModel = new PageRouteModel("/Pages/Test.cshtml", "/Pages/Test");

            context.RouteModels.Add(pageModel);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model => Assert.Same(pageModel, model),
                              model =>
            {
                Assert.Equal("/Pages/Home.cshtml", model.RelativePath);
                Assert.Equal("/Pages/Home", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Pages/Home", selector.AttributeRouteModel.Template));
            });
        }
コード例 #5
0
        public void OnProvidersExecuting_DiscoversFilesUnderBasePath()
        {
            // Arrange
            var fileSystem = new VirtualRazorProjectFileSystem();

            fileSystem.Add(new TestRazorProjectItem("/Pages/Index.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Pages/_Layout.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/NotPages/Index.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/NotPages/_Layout.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Index.cshtml", "@page"));

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/Pages";
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/Index.cshtml", model.RelativePath);
            });
        }
コード例 #6
0
        public void OnProvidersExecuting_ReturnsPagesWithPageDirective()
        {
            // Arrange
            var fileSystem = new VirtualRazorProjectFileSystem();

            fileSystem.Add(new TestRazorProjectItem("/Pages/Home.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Pages/Test.cshtml", "Hello world"));

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/";
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/Home.cshtml", model.RelativePath);
                Assert.Equal("/Pages/Home", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Pages/Home", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Pages/Home", kvp.Value);
                });
            });
        }
コード例 #7
0
        public void OnProvidersExecuting_DoesNotAddPagesUnderAreas_WhenFeatureIsDisabled()
        {
            // Arrange
            var fileSystem = new VirtualRazorProjectFileSystem();

            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Pages/Manage/Categories.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Pages/Index.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Pages/List.cshtml", "@page \"{sortOrder?}\""));
            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Pages/_ViewStart.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Pages/About.cshtml", "@page"));

            var optionsManager = Options.Create(new RazorPagesOptions {
                AllowAreas = false
            });
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/About.cshtml", model.RelativePath);
                Assert.Equal("/About", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("About", selector.AttributeRouteModel.Template));
            });
        }
コード例 #8
0
        public void OnProvidersExecuting_DoesNotAddAreaAndNonAreaRoutesForAPage()
        {
            // Arrange
            var fileSystem = new VirtualRazorProjectFileSystem();

            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Categories.cshtml", "@page"));
            // We shouldn't add a route for this.
            fileSystem.Add(new TestRazorProjectItem("/Areas/Home.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/About.cshtml", "@page"));

            var optionsManager = Options.Create(new RazorPagesOptions
            {
                RootDirectory     = "/",
                AreaRootDirectory = "/Areas",
                AllowAreas        = true,
            });
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Areas/Products/Categories.cshtml", model.RelativePath);
                Assert.Equal("/Categories", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Categories", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Categories", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/About.cshtml", model.RelativePath);
                Assert.Equal("/About", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("About", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/About", kvp.Value);
                });
            });
        }
コード例 #9
0
        public void OnProvidersExecuting_AddsMultipleSelectorsForIndexPages()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file1        = fileProvider.AddFile("/Pages/Index.cshtml", "@page");
            var file2        = fileProvider.AddFile("/Pages/Test.cshtml", "Hello world");
            var file3        = fileProvider.AddFile("/Pages/Admin/Index.cshtml", "@page \"test\"");

            var dir2 = fileProvider.AddDirectoryContent("/Pages/Admin", new[] { file3 });
            var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { dir2, file1, file2 });

            fileProvider.AddDirectoryContent("/", new[] { dir1 });

            var fileSystem = new TestRazorProjectFileSystem(fileProvider, _hostingEnvironment);

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/";
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/Admin/Index.cshtml", model.RelativePath);
                Assert.Equal("/Pages/Admin/Index", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Pages/Admin/Index/test", selector.AttributeRouteModel.Template),
                                  selector => Assert.Equal("Pages/Admin/test", selector.AttributeRouteModel.Template));
            },
                              model =>
            {
                Assert.Equal("/Pages/Index.cshtml", model.RelativePath);
                Assert.Equal("/Pages/Index", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Pages/Index", selector.AttributeRouteModel.Template),
                                  selector => Assert.Equal("Pages", selector.AttributeRouteModel.Template));
            });
        }
コード例 #10
0
        public void OnProvidersExecuting_ThrowsIfRouteTemplateHasOverridePattern()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file         = fileProvider.AddFile("/Index.cshtml", "@page \"/custom-route\"");

            fileProvider.AddDirectoryContent("/", new[] { file });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/";
            var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => provider.OnProvidersExecuting(context));

            Assert.Equal("The route for the page at '/Index.cshtml' cannot start with / or ~/. Pages do not support overriding the file path of the page.",
                         ex.Message);
        }
コード例 #11
0
        public void OnProvidersExecuting_DiscoversFilesUnderBasePath()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var dir1         = fileProvider.AddDirectoryContent("/Pages",
                                                                new[]
            {
                fileProvider.AddFile("/Pages/Index.cshtml", "@page"),
                fileProvider.AddFile("/Pages/_Layout.cshtml", "@page")
            });
            var dir2 = fileProvider.AddDirectoryContent("/NotPages",
                                                        new[]
            {
                fileProvider.AddFile("/NotPages/Index.cshtml", "@page"),
                fileProvider.AddFile("/NotPages/_Layout.cshtml", "@page")
            });
            var rootFile = fileProvider.AddFile("/Index.cshtml", "@page");

            fileProvider.AddDirectoryContent("/", new IFileInfo[] { rootFile, dir1, dir2 });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = Options.Create(new RazorPagesOptions());

            optionsManager.Value.RootDirectory = "/Pages";
            var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/Index.cshtml", model.RelativePath);
            });
        }
コード例 #12
0
        public void OnProvidersExecuting_DoesNotAddPagesUnderAreas_WhenFeatureIsDisabled()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file1        = fileProvider.AddFile("Categories.cshtml", "@page");
            var file2        = fileProvider.AddFile("Index.cshtml", "@page");
            var file3        = fileProvider.AddFile("List.cshtml", "@page \"{sortOrder?}\"");
            var file4        = fileProvider.AddFile("About.cshtml", "@page");
            var manageDir    = fileProvider.AddDirectoryContent("/Areas/Products/Pages/Manage", new[] { file1 });
            var areaPagesDir = fileProvider.AddDirectoryContent("/Areas/Products/Pages", new IFileInfo[] { manageDir, file2, file3, });
            var productsDir  = fileProvider.AddDirectoryContent("/Areas/Products", new[] { areaPagesDir });
            var areasDir     = fileProvider.AddDirectoryContent("/Areas", new[] { productsDir });
            var pagesDir     = fileProvider.AddDirectoryContent("/Pages", new[] { file4 });
            var rootDir      = fileProvider.AddDirectoryContent("/", new[] { areasDir, pagesDir });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = Options.Create(new RazorPagesOptions {
                AllowAreas = false
            });
            var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Pages/About.cshtml", model.RelativePath);
                Assert.Equal("/About", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("About", selector.AttributeRouteModel.Template));
            });
        }
コード例 #13
0
        public void OnProvidersExecuting_DoesNotAddPageDirectivesIfItAlreadyExists()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file1        = fileProvider.AddFile("/Pages/Home.cshtml", "@page");
            var file2        = fileProvider.AddFile("/Pages/Test.cshtml", "@page");

            var dir1 = fileProvider.AddDirectoryContent("/Pages", new IFileInfo[] { file1, file2 });

            fileProvider.AddDirectoryContent("/", new[] { dir1 });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = new TestOptionsManager <RazorPagesOptions>();

            optionsManager.Value.RootDirectory = "/";
            var provider  = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context   = new PageRouteModelProviderContext();
            var pageModel = new PageRouteModel("/Pages/Test.cshtml", "/Pages/Test");

            context.RouteModels.Add(pageModel);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model => Assert.Same(pageModel, model),
                              model =>
            {
                Assert.Equal("/Pages/Home.cshtml", model.RelativePath);
                Assert.Equal("/Pages/Home", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Pages/Home", selector.AttributeRouteModel.Template));
            });
        }
コード例 #14
0
        public void OnProvidersExecuting_AddsPagesUnderAreas()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var file1        = fileProvider.AddFile("Categories.cshtml", "@page");
            var file2        = fileProvider.AddFile("Index.cshtml", "@page");
            var file3        = fileProvider.AddFile("List.cshtml", "@page \"{sortOrder?}\"");
            var file4        = fileProvider.AddFile("_ViewStart.cshtml", "@page");
            var manageDir    = fileProvider.AddDirectoryContent("/Areas/Products/Pages/Manage", new[] { file1 });
            var pagesDir     = fileProvider.AddDirectoryContent("/Areas/Products/Pages", new IFileInfo[] { manageDir, file2, file3, file4 });
            var productsDir  = fileProvider.AddDirectoryContent("/Areas/Products", new[] { pagesDir });
            var areasDir     = fileProvider.AddDirectoryContent("/Areas", new[] { productsDir });
            var rootDir      = fileProvider.AddDirectoryContent("/", new[] { areasDir });

            var project = new TestRazorProject(fileProvider);

            var optionsManager = Options.Create(new RazorPagesOptions {
                AllowAreas = true
            });
            var provider = new RazorProjectPageRouteModelProvider(project, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/Manage/Categories.cshtml", model.RelativePath);
                Assert.Equal("/Manage/Categories", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Manage/Categories", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Manage/Categories", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/Index.cshtml", model.RelativePath);
                Assert.Equal("/Index", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Index", selector.AttributeRouteModel.Template),
                                  selector => Assert.Equal("Products", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Index", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/List.cshtml", model.RelativePath);
                Assert.Equal("/List", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/List/{sortOrder?}", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/List", kvp.Value);
                });
            });
        }
コード例 #15
0
        public void OnProvidersExecuting_DoesNotAddAreaAndNonAreaRoutesForAPage()
        {
            // Arrange
            var fileProvider = new TestFileProvider();
            var conformingFileUnderAreasDirectory = fileProvider.AddFile("Categories.cshtml", "@page");
            // We shouldn't add a route for this.
            var nonConformingFileUnderAreasDirectory = fileProvider.AddFile("Home.cshtml", "@page");
            var rootFile = fileProvider.AddFile("About.cshtml", "@page");

            var productsDir = fileProvider.AddDirectoryContent("/Areas/Products", new[] { conformingFileUnderAreasDirectory });
            var areasDir    = fileProvider.AddDirectoryContent("/Areas", new IFileInfo[] { productsDir, nonConformingFileUnderAreasDirectory });
            var rootDir     = fileProvider.AddDirectoryContent("/", new IFileInfo[] { areasDir, rootFile });

            var fileSystem = new TestRazorProjectFileSystem(fileProvider, _hostingEnvironment);

            var optionsManager = Options.Create(new RazorPagesOptions
            {
                RootDirectory     = "/",
                AreaRootDirectory = "/Areas",
                AllowAreas        = true,
            });
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Areas/Products/Categories.cshtml", model.RelativePath);
                Assert.Equal("/Categories", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Categories", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Categories", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/About.cshtml", model.RelativePath);
                Assert.Equal("/About", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("About", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/About", kvp.Value);
                });
            });
        }
コード例 #16
0
        public void OnProvidersExecuting_AddsPagesUnderAreas()
        {
            // Arrange
            var fileSystem = new VirtualRazorProjectFileSystem();

            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Pages/Manage/Categories.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Pages/Index.cshtml", "@page"));
            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Pages/List.cshtml", "@page \"{sortOrder?}\""));
            fileSystem.Add(new TestRazorProjectItem("/Areas/Products/Pages/_ViewStart.cshtml", "@page"));

            var optionsManager = Options.Create(new RazorPagesOptions {
                AllowAreas = true
            });
            var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance);
            var context  = new PageRouteModelProviderContext();

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(context.RouteModels,
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/Index.cshtml", model.RelativePath);
                Assert.Equal("/Index", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Index", selector.AttributeRouteModel.Template),
                                  selector => Assert.Equal("Products", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Index", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/List.cshtml", model.RelativePath);
                Assert.Equal("/List", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/List/{sortOrder?}", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/List", kvp.Value);
                });
            },
                              model =>
            {
                Assert.Equal("/Areas/Products/Pages/Manage/Categories.cshtml", model.RelativePath);
                Assert.Equal("/Manage/Categories", model.ViewEnginePath);
                Assert.Collection(model.Selectors,
                                  selector => Assert.Equal("Products/Manage/Categories", selector.AttributeRouteModel.Template));
                Assert.Collection(model.RouteValues.OrderBy(k => k.Key),
                                  kvp =>
                {
                    Assert.Equal("area", kvp.Key);
                    Assert.Equal("Products", kvp.Value);
                },
                                  kvp =>
                {
                    Assert.Equal("page", kvp.Key);
                    Assert.Equal("/Manage/Categories", kvp.Value);
                });
            });
        }