Make error handling consistent

This commit is contained in:
Laura Hausmann 2022-11-26 17:17:38 +01:00
parent 21ec2ad89d
commit e53a83f1d2
Signed by: zotan
GPG key ID: D044E84C5BE01605
3 changed files with 43 additions and 26 deletions

View file

@ -1,25 +1,21 @@
@page "/blog/{post}" @page "/blog/{post}"
@model BlogPostModel
@using zotanpw.Backend @using zotanpw.Backend
@{ @{
if (string.IsNullOrWhiteSpace((string)RouteData.Values["post"]!)) { if (Model.Post == null)
return; return;
}
var post = BlogModel.Posts.FirstOrDefault(p => p.Shorthand == (string)RouteData.Values["post"]!);
if (post == null) {
Response.Redirect("/Error");
return;
}
ViewData["title"] = "blog"; ViewData["title"] = "blog";
ViewData["subtitle"] = post.Shorthand; ViewData["subtitle"] = Model.Post.Shorthand;
ViewData["desc"] = post.Title; ViewData["desc"] = Model.Post.Title;
#if (DEBUG) #if (DEBUG)
post.UpdateContentAndMetadata(); Model.Post.UpdateContentAndMetadata();
#endif #endif
} }
<b>@post.PublishedOn.ToString("yyyy-MM-dd")</b> - @Utils.a_an(post.ReadTimeMinutes) @post.ReadTimeMinutes minute read (150 wpm) <b>@Model.Post.PublishedOn.ToString("yyyy-MM-dd")</b> - @Utils.a_an(Model.Post.ReadTimeMinutes) @Model.Post.ReadTimeMinutes minute read (150 wpm)
<h1 id="post">@post.Title</h1> <h1 id="post">@Model.Post.Title</h1>
<div style="text-align: justify"> <div style="text-align: justify">
@Html.Raw(post.Content) @Html.Raw(Model.Post.Content)
</div> </div>

View file

@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace zotanpw.Pages.blog;
public class BlogPostModel : PageModel {
public BlogModel.BlogPost? Post;
public IActionResult OnGet() {
if (string.IsNullOrWhiteSpace((string)RouteData.Values["post"]!)) {
return NotFound();
}
Post = BlogModel.Posts.FirstOrDefault(p => p.Shorthand == (string)RouteData.Values["post"]!);
if (Post == null) {
return NotFound();
}
return Page();
}
}