private void AddAreaPageModels(PageRouteModelProviderContext context)
        {
            foreach (var item in _razorFileSystem.EnumerateItems(AreaRootDirectory))
            {
                var relativePath = item.CombinedPath;
                if (context.RouteModels.Any(m => string.Equals(relativePath, m.RelativePath, StringComparison.OrdinalIgnoreCase)))
                {
                    // A route for this file was already registered either by the CompiledPageRouteModel.
                    // Skip registering an additional entry.
                    continue;
                }

                if (!PageDirectiveFeature.TryGetPageDirective(_logger, item, out var routeTemplate))
                {
                    // .cshtml pages without @page are not RazorPages.
                    continue;
                }

                var routeModel = _routeModelFactory.CreateAreaRouteModel(relativePath, routeTemplate);
                if (routeModel != null)
                {
                    context.RouteModels.Add(routeModel);
                }
            }
        }
Пример #2
0
        public void CreateAreaRouteModel_AddsSelector()
        {
            // Arrange
            var relativePath      = "/Areas/TestArea/Pages/Users/Profile.cshtml";
            var options           = new RazorPagesOptions();
            var routeModelFactory = new PageRouteModelFactory(options, NullLogger.Instance);

            // Act
            var routeModel = routeModelFactory.CreateAreaRouteModel(relativePath, "{id?}");

            // Assert
            Assert.Equal(relativePath, routeModel.RelativePath);
            Assert.Equal("/Users/Profile", routeModel.ViewEnginePath);
            Assert.Equal("TestArea", routeModel.AreaName);

            Assert.Collection(
                routeModel.Selectors,
                selector => Assert.Equal("TestArea/Users/Profile/{id?}", selector.AttributeRouteModel.Template));

            Assert.Collection(
                routeModel.RouteValues.OrderBy(kvp => kvp.Key),
                kvp =>
            {
                Assert.Equal("area", kvp.Key);
                Assert.Equal("TestArea", kvp.Value);
            },
                kvp =>
            {
                Assert.Equal("page", kvp.Key);
                Assert.Equal("/Users/Profile", kvp.Value);
            });
        }
        private void CreateModels(PageRouteModelProviderContext context)
        {
            var rootDirectory = _pagesOptions.RootDirectory;

            if (!rootDirectory.EndsWith("/", StringComparison.Ordinal))
            {
                rootDirectory = rootDirectory + "/";
            }

            var areaRootDirectory = "/Areas/";

            foreach (var viewDescriptor in GetViewDescriptors(_applicationManager))
            {
                var            relativePath  = viewDescriptor.RelativePath;
                var            routeTemplate = GetRouteTemplate(viewDescriptor);
                PageRouteModel routeModel    = null;

                // When RootDirectory and AreaRootDirectory overlap (e.g. RootDirectory = '/', AreaRootDirectory = '/Areas'), we
                // only want to allow a page to be associated with the area route.
                if (relativePath.StartsWith(areaRootDirectory, StringComparison.OrdinalIgnoreCase))
                {
                    routeModel = _routeModelFactory.CreateAreaRouteModel(relativePath, routeTemplate);
                }
                else if (relativePath.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase))
                {
                    routeModel = _routeModelFactory.CreateRouteModel(relativePath, routeTemplate);
                }

                if (routeModel != null)
                {
                    context.RouteModels.Add(routeModel);
                }
            }
        }
Пример #4
0
        private void CreateModels(PageRouteModelProviderContext context)
        {
            var rootDirectory = _pagesOptions.RootDirectory;

            if (!rootDirectory.EndsWith("/", StringComparison.Ordinal))
            {
                rootDirectory = rootDirectory + "/";
            }

            var areaRootDirectory = "/Areas/";

            foreach (var viewDescriptor in GetViewDescriptors(_applicationManager))
            {
                if (viewDescriptor.Item != null && !ChecksumValidator.IsItemValid(_razorProjectEngine.FileSystem, viewDescriptor.Item))
                {
                    // If we get here, this compiled Page has different local content, so ignore it.
                    continue;
                }

                var            relativePath  = viewDescriptor.RelativePath;
                var            routeTemplate = GetRouteTemplate(viewDescriptor);
                PageRouteModel routeModel    = null;

                // When RootDirectory and AreaRootDirectory overlap (e.g. RootDirectory = '/', AreaRootDirectory = '/Areas'), we
                // only want to allow a page to be associated with the area route.
                if (_pagesOptions.AllowAreas && relativePath.StartsWith(areaRootDirectory, StringComparison.OrdinalIgnoreCase))
                {
                    routeModel = _routeModelFactory.CreateAreaRouteModel(relativePath, routeTemplate);
                }
                else if (relativePath.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase))
                {
                    routeModel = _routeModelFactory.CreateRouteModel(relativePath, routeTemplate);
                }

                if (routeModel != null)
                {
                    context.RouteModels.Add(routeModel);
                }
            }
        }