Managed page rendering

This commit is contained in:
2025-11-21 01:41:25 +08:00
parent 226a64df41
commit cad72502d9
2 changed files with 21 additions and 28 deletions

View File

@@ -8,12 +8,15 @@
<div class="h-screen flex justify-center items-center"> <div class="h-screen flex justify-center items-center">
<div class="text-center max-w-96"> <div class="text-center max-w-96">
<img src="~/favicon.png" width="80" height="80" alt="Logo" class="mb-1 mx-auto"/> <img src="~/favicon.png" width="80" height="80" alt="Logo" class="mb-1 mx-auto"/>
<h1 class="text-2xl">Hello, World 👋</h1> <h1 class="text-2xl">404 Not Found</h1>
<p>Here are the Solar Network Pages</p> <p>Here are the Solar Network Pages</p>
<p>And you're accessing the site <b>@ViewData["SiteName"]</b></p> <p>And you're accessing the site <b>@(Model.SiteName ?? "main")</b></p>
<p class="text-sm opacity-80 mt-1"> <p class="text-sm opacity-80 mt-1">
The reason you're seeing this is the author of the site The reason you're seeing this is the author of the site
haven't configure anything that match this route. haven't configure anything that match this route.
</p> </p>
<div class="text-xs opacity-70 mt-1">
<p>Path: <b>@Model.CurrentPath</b> Site ID: <b>@(Model.Site?.Id.ToString() ?? "none")</b></p>
</div>
</div> </div>
</div> </div>

View File

@@ -2,17 +2,22 @@ using DysonNetwork.Shared.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.IO;
using System.Text.Json;
namespace DysonNetwork.Zone.Pages; namespace DysonNetwork.Zone.Pages;
public class IndexModel(AppDatabase db) : PageModel public class IndexModel(AppDatabase db) : PageModel
{ {
public SnPublicationSite? Site { get; set; }
public string? SiteName { get; set; } public string? SiteName { get; set; }
public string? CurrentPath { get; set; }
public async Task<IActionResult> OnGet(string? path = null) public async Task<IActionResult> OnGet(string? path = null)
{ {
var siteNameValue = Request.Headers["X-SiteName"].ToString(); var siteNameValue = Request.Headers["X-SiteName"].ToString();
SiteName = siteNameValue; SiteName = siteNameValue;
CurrentPath = Path.Combine("/", path ?? "");
if (string.IsNullOrEmpty(siteNameValue)) if (string.IsNullOrEmpty(siteNameValue))
{ {
SiteName = null; SiteName = null;
@@ -20,36 +25,21 @@ public class IndexModel(AppDatabase db) : PageModel
else else
{ {
var capturedName = siteNameValue; var capturedName = siteNameValue;
var site = await db.PublicationSites.FirstOrDefaultAsync(s => Site = await db.PublicationSites.FirstOrDefaultAsync(s => s.Slug == capturedName);
s.Name == capturedName && s.Mode == PublicationSiteMode.FullyManaged); if (Site == null) return Page();
if (site != null) var pagePath = CurrentPath;
{
var pagePath = "/" + (path ?? "");
if (pagePath == "/") pagePath = "/";
var page = await db.PublicationPages.FirstOrDefaultAsync(p => var page = await db.PublicationPages.FirstOrDefaultAsync(p => p.SiteId == Site.Id && p.Path == pagePath);
p.SiteId == site.Id && p.Path == pagePath); if (page == null) return Page();
if (page != null) switch (page.Type)
{ {
if (page.Type == PublicationPageType.HtmlPage) case PublicationPageType.HtmlPage when page.Config.TryGetValue("html", out var html) && html is JsonElement content:
{ return Content(content.ToString(), "text/html");
if (page.Config.TryGetValue("html", out var html) && html is string content) case PublicationPageType.Redirect when page.Config.TryGetValue("url", out var url) && url is JsonElement redirectUrl:
{ return Redirect(redirectUrl.ToString());
return Content(content, "text/html");
}
}
else if (page.Type == PublicationPageType.Redirect)
{
if (page.Config.TryGetValue("url", out var url) && url is string redirectUrl)
{
return Redirect(redirectUrl);
}
}
}
} }
} }
return Page(); return Page();
} }
} }