From e53a83f1d202b97671dac91ee7fbcd6819ea6561 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Sat, 26 Nov 2022 17:17:38 +0100 Subject: [PATCH] Make error handling consistent --- Pages/blog/BlogPost.cshtml | 22 +++++++++------------- Pages/blog/BlogPost.cshtml.cs | 21 +++++++++++++++++++++ zotan.pw-web.csproj | 26 +++++++++++++------------- 3 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 Pages/blog/BlogPost.cshtml.cs diff --git a/Pages/blog/BlogPost.cshtml b/Pages/blog/BlogPost.cshtml index 13c4dcc..1be7361 100644 --- a/Pages/blog/BlogPost.cshtml +++ b/Pages/blog/BlogPost.cshtml @@ -1,25 +1,21 @@ @page "/blog/{post}" +@model BlogPostModel @using zotanpw.Backend @{ - if (string.IsNullOrWhiteSpace((string)RouteData.Values["post"]!)) { + if (Model.Post == null) return; - } - var post = BlogModel.Posts.FirstOrDefault(p => p.Shorthand == (string)RouteData.Values["post"]!); - if (post == null) { - Response.Redirect("/Error"); - return; - } + ViewData["title"] = "blog"; - ViewData["subtitle"] = post.Shorthand; - ViewData["desc"] = post.Title; + ViewData["subtitle"] = Model.Post.Shorthand; + ViewData["desc"] = Model.Post.Title; #if (DEBUG) - post.UpdateContentAndMetadata(); + Model.Post.UpdateContentAndMetadata(); #endif } -@post.PublishedOn.ToString("yyyy-MM-dd") - @Utils.a_an(post.ReadTimeMinutes) @post.ReadTimeMinutes minute read (150 wpm) -

@post.Title

+@Model.Post.PublishedOn.ToString("yyyy-MM-dd") - @Utils.a_an(Model.Post.ReadTimeMinutes) @Model.Post.ReadTimeMinutes minute read (150 wpm) +

@Model.Post.Title

- @Html.Raw(post.Content) + @Html.Raw(Model.Post.Content)
diff --git a/Pages/blog/BlogPost.cshtml.cs b/Pages/blog/BlogPost.cshtml.cs new file mode 100644 index 0000000..82b82fa --- /dev/null +++ b/Pages/blog/BlogPost.cshtml.cs @@ -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(); + } +} diff --git a/zotan.pw-web.csproj b/zotan.pw-web.csproj index e532e8f..22e946f 100644 --- a/zotan.pw-web.csproj +++ b/zotan.pw-web.csproj @@ -8,32 +8,32 @@ - - - - - - - + + + + + + + - + - - - + + + - <_ContentIncludedByDefault Remove="Pages\ErrorPages\404.cshtml"/> + <_ContentIncludedByDefault Remove="Pages\ErrorPages\404.cshtml" /> - +