Unable to create a new Page type in 8.3.0
See original GitHub issueHi,
I’ve just started playing around with Piranha CMS, which seems lika a rellay great CMS tool that I would like to use. After reading the docs I wanted to get a feeling of what it is like to use Piranha as a programmer and the first thing I wanted to try was to create a new Page type. But I am unable to get it to work!
I’m using the latest NuGet packages (8.3.0) and created a project using the piranha.razor template (after installing it with dotnet new -i Piranha.Templates
as per the docs. I then made a copy of Models\StandardPage.cs and renamed the file to TestPage.cs:
using Piranha.AttributeBuilder;
using Piranha.Models;
namespace jonasblog.Models
{
[PageType(Title = "Test page")]
public class TestPage : Page<TestPage>
{
}
}
I run in debug with Visual Studio 2019, add seed data and login in to the manager. In the manager I can select “Test page” when adding a new page. I add a title to the page and some blocks, saves and publishes. When I then go to the front page of the site I can see the newly created page in the menu, but when I click the link I get a 404 error. The URL matches the slug in the page settings URL segment.
If I do the same thing, but use Standard Page Type instead of my Test Page it works just fine!
In the docs it says that when creating a new page type, it should be added in the Configure method of the startup class using the PageTypeBuilder, but it also says that one could use the ContentTypeBuilder (new ContentTypeBuilder(api).AddAssembly(typeof(Startup).Assembly).Build().DeleteOrphans();
) which is specified in the template. Is this not true, are the docs out of date? Is it possible to use both PageTypeBuilder and ContentTypeBuilder to get around this issue? If so, what order should the be added in?
I’m probably doing something wrong, but what? Since I can see the Page Type in the manager I’m guessing that the page type is added correctly, so I can’t be that far off.
Thanks in advance, and I hope this is the correct place to post this kind of question!
Hälsningar, Jonas
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Hi there @jonaslorander! There’s nothing wrong with the page type itself, but you haven’t configured any
routing
for your new type. When a page matching the current request url is found by the middleware is found it’s routed to mvc/razor pages. The default route for pages is/page
, but as you see by taking a look at~/Pages/Page.cshtml
(https://github.com/PiranhaCMS/piranha.core.templates/blob/master/src/web/razor/Pages/Page.cshtml) this expects aStandardPage
as it’s model.To fix this you need to send all requests to pages of your new type to another
.cshtml
page. This is done by adding thePageTypeRouteAttribute
to your type, for example:Then to serve the request you would add a page at
~/Pages/Test.cshtml
. You can make a copy of~/Pages/Page.cshtml
, but remember to change the model type at the second line.You can find more information about routing on the page under “Page Routing” https://piranhacms.org/docs/content/pages
@tidyui it was because my view filename was Contact.cshtml, when renamed to ContactPage.cshtml it workd. Thanks!