Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StaticPage always null #7

Open
kttary opened this issue Aug 16, 2023 · 1 comment
Open

StaticPage always null #7

kttary opened this issue Aug 16, 2023 · 1 comment

Comments

@kttary
Copy link

kttary commented Aug 16, 2023

Following #6 i still unable to display md file. Here is my setup.

program.cs

...
builder.Services.AddStatica(
	new Statica.Models.StaticStructure {
		Id = "help",
		DataPath = Path.Combine("wwwroot", "help"),
		BaseSlug = "help",
		UseAssets = true
	});

builder.Services.AddPiranha(options =>
{
...
}
...
builder.Services.AddRazorPages(options => {
    options.RootDirectory = "/Pages";
    options.Conventions.AddPageRoute("/HelpMe", "help");
});

var app = builder.Build(); 

app.UseStaticFiles();
app.UseStatica(app.Environment);
...
app.MapRazorPages();
app.Run();

HelpMe.cshtml

@page "/help/{**slug}"
@using Statica.Models;
@model Help
@inject Statica.Services.IStaticaService Statica
@{
    ViewData["Title"] = Model.StaticPage != null ? Model.StaticPage.Title : "Not Found";
    ViewData["StaticSlug"] = Model.StaticPage?.Slug;

    ViewData["content"] = @Model.StaticPage == null ? "</h5>NULL</h5>" : @Html.Raw(Model.StaticPage.Body);

    var structures = Statica.GetStructures();
}

<div>
   @foreach (var st in structures)
   {
       @foreach (var i in st.Sitemap.Items) 
       {
           @foreach (StaticPage sp in i.Items) 
           {
                <h4>@sp.Path : @sp.Slug</h4>
           }
       }
   }
   @ViewData["content"]
</div>

Help.cs

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Piranha;
using Statica.Models;
using Statica.Services;

public class Help : PageModel
{
    private readonly IStaticaService _service;
    public string StaticId { get; set; }
    public StaticSitemap StaticSitemap { get; set; }
    public StaticPageModel StaticPage { get; set; }
    public string Slug { get; set; }

    public Help(IStaticaService service)
    {
        _service = service;
    }

    public async Task<IActionResult> OnGetAsync(Guid id, string slug)
    {
        string[] segments = new string[0];

        if (!string.IsNullOrEmpty(slug))
        {
            segments = slug.Split("/", options: StringSplitOptions.RemoveEmptyEntries);
        }

        var structure = segments.Length > 0 ?
            _service.GetStructure(segments[0]) :
            _service.GetStructure("help");

        if (structure != null)
        {
            StaticId = structure.Id;
            StaticSitemap = structure.Sitemap;

            if (segments.Length > 1)
            {
                Slug = string.Join("/", segments.Subset(1));
                StaticPage = await structure.GetPageAsync(Slug);

                if (StaticPage != null && !string.IsNullOrWhiteSpace(StaticPage.Redirect))
                {
                    return Redirect($"~/{ StaticPage.Redirect }");
                }
            }
        }
        return Page();
    }
}

The md filles is in www folder with following structure:

wwwroot
   - help
        - 01. Part1
             - 01. Basics
                  - 01. What Is Piranha.md
                  - 02. Prerequisites.md
                  - etc
             - 02. Content
             - 03. Application
             - 04. Architecture
        - 02. Part2
             - 05, Manager Architecture
             - 06. Extensions
             - etc
        - _assets

Now if i hit http://localhost:500/help it shows some slug.
image
The problem is @model.StaticPage is always null so i can't render the StaticPage body. I tried some variation like http://localhost:5000/help/part1/basics/what-is-piranha still no luck.

@kttary
Copy link
Author

kttary commented Aug 18, 2023

Finally i manage to get it work. But there are another two issue:

  1. Statica 2.0 is not compatible with newest piranha. Piranha 10.3.0 throw an error 'Could not load type 'Piranha.IInitializable' from assembly 'Piranha, Version=10.3.0.0, Culture=neutral, PublicKeyToken=null'.'
  2. Makdown table not rendered properly. For example the Main Vue Apps table and the Reusable Vue Apps table on the manager-architecture/introduction.md files is rendered as paragraph as shown below:
    image
    It is supposed to be a table on this page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant