private void AddAreaPageModels(PageRouteModelProviderContext context)
        {
            foreach (var item in _project.EnumerateItems(_pagesOptions.AreaRootDirectory))
            {
                if (!IsRouteable(item))
                {
                    continue;
                }

                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);
                }
            }
        }
        private void AddPageModels(PageRouteModelProviderContext context)
        {
            foreach (var item in _project.EnumerateItems(_pagesOptions.RootDirectory))
            {
                if (!IsRouteable(item))
                {
                    continue;
                }

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

                var routeModel = new PageRouteModel(
                    relativePath: item.CombinedPath,
                    viewEnginePath: item.FilePathWithoutExtension);

                if (IsAlreadyRegistered(context, routeModel))
                {
                    // The CompiledPageRouteModelProvider (or another provider) already registered a PageRoute for this path.
                    // Don't register a duplicate entry for this route.
                    continue;
                }

                PageSelectorModel.PopulateDefaults(routeModel, routeModel.ViewEnginePath, routeTemplate);
                context.RouteModels.Add(routeModel);
            }
        }
Esempio n. 3
0
    private void AddPageModels(PageRouteModelProviderContext context)
    {
        foreach (var item in _razorFileSystem.EnumerateItems(_pagesOptions.RootDirectory))
        {
            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 or as an area route.
                // by this provider. Skip registering an additional entry.

                // Note: We're comparing duplicates based on root-relative paths. This eliminates a page from being discovered
                // by overlapping area and non-area routes where ViewEnginePath would be different.
                continue;
            }

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

            var routeModel = _routeModelFactory.CreateRouteModel(relativePath, routeTemplate);
            if (routeModel != null)
            {
                context.RouteModels.Add(routeModel);
            }
        }
    }
        public void OnProvidersExecuting(PageRouteModelProviderContext context)
        {
            foreach (var item in _project.EnumerateItems(_pagesOptions.RootDirectory))
            {
                if (item.FileName.StartsWith("_"))
                {
                    // Pages like _ViewImports should not be routable.
                    continue;
                }

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

                if (IsAlreadyRegistered(context, item))
                {
                    // The CompiledPageRouteModelProvider (or another provider) already registered a PageRoute for this path.
                    // Don't register a duplicate entry for this route.
                    continue;
                }

                var routeModel = new PageRouteModel(
                    relativePath: item.CombinedPath,
                    viewEnginePath: item.FilePathWithoutExtension);
                PageSelectorModel.PopulateDefaults(routeModel, routeTemplate);

                context.RouteModels.Add(routeModel);
            }
        }
        private void AddPageModels(PageRouteModelProviderContext context)
        {
            var normalizedAreaRootDirectory = Startup.OtherRootDirectory;

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

            foreach (var item in _project.EnumerateItems(Startup.OtherRootDirectory))
            {
                if (!IsRouteable(item))
                {
                    continue;
                }

                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 or as an area route.
                    // by this provider. Skip registering an additional entry.

                    // Note: We're comparing duplicates based on root-relative paths. This eliminates a page from being discovered
                    // by overlapping area and non-area routes where ViewEnginePath would be different.
                    continue;
                }

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

                //if (_pagesOptions.AllowAreas && relativePath.StartsWith(normalizedAreaRootDirectory, StringComparison.OrdinalIgnoreCase))
                //{
                //    // Ignore Razor pages that are under the area root directory when AllowAreas is enabled.
                //    // Conforming page paths will be added by AddAreaPageModels.
                //    //_logger.UnsupportedAreaPath(_pagesOptions, relativePath);
                //    continue;
                //}

                var routeModel = _routeModelFactory.CreateRouteModel(relativePath, routeTemplate);
                if (routeModel != null && context.RouteModels.Any(m => string.Equals(routeModel.ViewEnginePath, m.ViewEnginePath, StringComparison.OrdinalIgnoreCase)) == false)
                {
                    context.RouteModels.Add(routeModel);
                }
            }
        }
        private void AddPageModels(PageRouteModelProviderContext context)
        {
            var normalizedAreaRootDirectory = _pagesOptions.AreaRootDirectory;

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

            foreach (var item in _project.EnumerateItems(_pagesOptions.RootDirectory))
            {
                if (!IsRouteable(item))
                {
                    continue;
                }

                var relativePath = item.CombinedPath;
                if (IsAlreadyRegistered(context, relativePath))
                {
                    // A route for this file was already registered either by the CompiledPageRouteModel or as an area route.
                    // by this provider. Skip registering an additional entry.
                    continue;
                }

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

                if (_pagesOptions.AllowAreas && relativePath.StartsWith(normalizedAreaRootDirectory, StringComparison.OrdinalIgnoreCase))
                {
                    // Ignore Razor pages that are under the area root directory when AllowAreas is enabled.
                    // Conforming page paths will be added by AddAreaPageModels.
                    _logger.UnsupportedAreaPath(_pagesOptions, relativePath);
                    continue;
                }

                var routeModel = new PageRouteModel(relativePath, viewEnginePath: item.FilePathWithoutExtension);
                PageSelectorModel.PopulateDefaults(routeModel, routeModel.ViewEnginePath, routeTemplate);
                context.RouteModels.Add(routeModel);
            }
        }
        private void AddAreaPageModels(PageRouteModelProviderContext context)
        {
            foreach (var item in _project.EnumerateItems(_pagesOptions.AreaRootDirectory))
            {
                if (!IsRouteable(item))
                {
                    continue;
                }

                var relativePath = item.CombinedPath;
                if (IsAlreadyRegistered(context, relativePath))
                {
                    // 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;
                }

                if (!PageSelectorModel.TryParseAreaPath(_pagesOptions, item.FilePath, _logger, out var areaResult))
                {
                    continue;
                }

                var routeModel = new PageRouteModel(relativePath, viewEnginePath: areaResult.viewEnginePath)
                {
                    RouteValues =
                    {
                        ["area"] = areaResult.areaName,
                    },
                };

                PageSelectorModel.PopulateDefaults(routeModel, areaResult.pageRoute, routeTemplate);
                context.RouteModels.Add(routeModel);
            }
        }