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); } } }
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); }); }