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); } } }
public void CreateRouteModel_AddsSelector() { // Arrange var relativePath = "/Pages/Users/Profile.cshtml"; var options = new RazorPagesOptions(); var routeModelFactory = new PageRouteModelFactory(options, NullLogger.Instance); // Act var routeModel = routeModelFactory.CreateRouteModel(relativePath, "{id?}"); // Assert Assert.Equal(relativePath, routeModel.RelativePath); Assert.Equal("/Users/Profile", routeModel.ViewEnginePath); Assert.Null(routeModel.AreaName); Assert.Collection( routeModel.Selectors, selector => Assert.Equal("Users/Profile/{id?}", selector.AttributeRouteModel.Template)); Assert.Collection( routeModel.RouteValues, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Users/Profile", kvp.Value); }); }
public void CreateRouteModel_DoesNotAddMultipleSelectorsWhenRouteTemplateIsAbsolute() { // Arrange var relativePath = "/Pages/Users/Profile/Index.cshtml"; var options = new RazorPagesOptions(); var routeModelFactory = new PageRouteModelFactory(options, NullLogger.Instance); // Act var routeModel = routeModelFactory.CreateRouteModel(relativePath, "/my-override"); // Assert Assert.Equal(relativePath, routeModel.RelativePath); Assert.Equal("/Users/Profile/Index", routeModel.ViewEnginePath); Assert.Collection( routeModel.Selectors, selector => Assert.Equal("my-override", selector.AttributeRouteModel.Template)); Assert.Collection( routeModel.RouteValues, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Users/Profile/Index", kvp.Value); }); }
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); } } }