diff --git a/core/Piranha.AttributeBuilder/PageTypeBuilder.cs b/core/Piranha.AttributeBuilder/PageTypeBuilder.cs index b3673aeac..6486be0a3 100644 --- a/core/Piranha.AttributeBuilder/PageTypeBuilder.cs +++ b/core/Piranha.AttributeBuilder/PageTypeBuilder.cs @@ -128,11 +128,20 @@ protected override PageType GetContentType(Type type) foreach (PageTypeRouteAttribute route in routes) { if (!string.IsNullOrWhiteSpace(route.Title) && !string.IsNullOrWhiteSpace(route.Route)) - pageType.Routes.Add(new ContentTypeRoute + { + var contentRoute = new ContentTypeRoute { Title = route.Title, Route = route.Route - }); + }; + + // Make sure the route starts with a forward slash + if (!contentRoute.Route.StartsWith("/")) + { + contentRoute.Route = $"/{ contentRoute.Route }"; + } + pageType.Routes.Add(contentRoute); + } } // Add default custom editors diff --git a/core/Piranha.AttributeBuilder/PostTypeBuilder.cs b/core/Piranha.AttributeBuilder/PostTypeBuilder.cs index 6957ddd0b..964210fba 100644 --- a/core/Piranha.AttributeBuilder/PostTypeBuilder.cs +++ b/core/Piranha.AttributeBuilder/PostTypeBuilder.cs @@ -128,11 +128,18 @@ protected override PostType GetContentType(Type type) { if (!string.IsNullOrWhiteSpace(route.Title) && !string.IsNullOrWhiteSpace(route.Route)) { - postType.Routes.Add(new ContentTypeRoute + var contentRoute = new ContentTypeRoute { Title = route.Title, Route = route.Route - }); + }; + + // Make sure the route starts with a forward slash + if (!contentRoute.Route.StartsWith("/")) + { + contentRoute.Route = $"/{ contentRoute.Route }"; + } + postType.Routes.Add(contentRoute); } } diff --git a/test/Piranha.Tests/AttributeBuilder/TypeBuilderTests.cs b/test/Piranha.Tests/AttributeBuilder/TypeBuilderTests.cs index 5a8b1016f..656c79f92 100644 --- a/test/Piranha.Tests/AttributeBuilder/TypeBuilderTests.cs +++ b/test/Piranha.Tests/AttributeBuilder/TypeBuilderTests.cs @@ -27,6 +27,12 @@ public class SimplePageType public Extend.Fields.TextField Body { get; set; } } + [PageType(Id = "Routed", Title = "Routed Page Type")] + [PageTypeRoute(Title = "Default", Route = "pageroute")] + public class RoutedPageType + { + } + [PageType(Id = "Archive", Title = "Archive Page Type", UseBlocks = false, IsArchive = true)] [PageTypeArchiveItem(typeof(SimplePostType))] public class ArchivePageType @@ -64,6 +70,12 @@ public class SimplePostType public Extend.Fields.TextField Body { get; set; } } + [PostType(Id = "Routed", Title = "Routed Post Type")] + [PostTypeRoute(Title = "Default", Route = "postroute")] + public class RoutedPostType + { + } + [PostType(Id = "Complex", Title = "Complex Post Type")] [PostTypeRoute(Title = "Default", Route = "/complex")] [PostTypeEditor(Title = "Custom Editor", Component = "will be replaced", Icon = "will be replaced")] @@ -159,6 +171,24 @@ public async Task AddSimplePageType() } } + [Fact] + public async Task AddForwardSlashToPageRoutes() + { + using (var api = CreateApi()) + { + var builder = new PageTypeBuilder(api) + .AddType(typeof(RoutedPageType)); + builder.Build(); + + var type = await api.PageTypes.GetByIdAsync("Routed"); + + Assert.NotNull(type); + + Assert.NotEmpty(type.Routes); + Assert.StartsWith("/", type.Routes[0]); + } + } + [Fact] public async Task AddArchivePageType() { @@ -241,6 +271,24 @@ public async Task AddSimplePostType() } } + [Fact] + public async Task AddForwardSlashToPostRoutes() + { + using (var api = CreateApi()) + { + var builder = new PostTypeBuilder(api) + .AddType(typeof(RoutedPostType)); + builder.Build(); + + var type = await api.PostTypes.GetByIdAsync("Routed"); + + Assert.NotNull(type); + + Assert.NotEmpty(type.Routes); + Assert.StartsWith("/", type.Routes[0]); + } + } + [Fact] public async Task AddComplexPostType() {