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();
}
}

View file

@ -8,32 +8,32 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="linq2db" Version="4.3.0"/> <PackageReference Include="linq2db" Version="4.3.0" />
<PackageReference Include="Markdig" Version="0.30.4"/> <PackageReference Include="Markdig" Version="0.30.4" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.0"/> <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="7.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0"/> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.0" />
<PackageReference Include="System.Data.SQLite" Version="1.0.115"/> <PackageReference Include="System.Data.SQLite" Version="1.0.115" />
<PackageReference Include="System.ServiceModel.Syndication" Version="7.0.0"/> <PackageReference Include="System.ServiceModel.Syndication" Version="7.0.0" />
<PackageReference Include="YamlDotNet" Version="12.0.2"/> <PackageReference Include="YamlDotNet" Version="12.0.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Pages\blog\posts"/> <Folder Include="Pages\blog\posts" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Remove=".gitignore"/> <None Remove=".gitignore" />
<None Remove=".bearer_token"/> <None Remove=".bearer_token" />
<None Remove="database.db"/> <None Remove="database.db" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<_ContentIncludedByDefault Remove="Pages\ErrorPages\404.cshtml"/> <_ContentIncludedByDefault Remove="Pages\ErrorPages\404.cshtml" />
</ItemGroup> </ItemGroup>
<Target Name="SetSourceRevisionId" BeforeTargets="InitializeSourceControlInformation"> <Target Name="SetSourceRevisionId" BeforeTargets="InitializeSourceControlInformation">
<Exec Command="git describe --long --always --dirty --exclude=* --abbrev=8" ConsoleToMSBuild="True" IgnoreExitCode="False"> <Exec Command="git describe --long --always --dirty --exclude=* --abbrev=8" ConsoleToMSBuild="True" IgnoreExitCode="False">
<Output PropertyName="SourceRevisionId" TaskParameter="ConsoleOutput"/> <Output PropertyName="SourceRevisionId" TaskParameter="ConsoleOutput" />
</Exec> </Exec>
</Target> </Target>