From 6c905f177f90b1293060a8be7cf74d30794cbc0c Mon Sep 17 00:00:00 2001 From: Robert Marshall Date: Sun, 28 Apr 2019 11:51:42 +0100 Subject: [PATCH] Throw 404 when blog post fails to load. --- Website/Controllers/BlogController.cs | 10 +++++++--- Website/Startup.cs | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Website/Controllers/BlogController.cs b/Website/Controllers/BlogController.cs index f0f76d9..92f8a0f 100644 --- a/Website/Controllers/BlogController.cs +++ b/Website/Controllers/BlogController.cs @@ -30,9 +30,13 @@ namespace Website.Controllers public async Task Entry(string url) { - var post = await _repo.GetPostByUrlAsync(url); - var model = new BlogPostViewModel(post); - return View(model); + try { + var post = await _repo.GetPostByUrlAsync(url); + var model = new BlogPostViewModel(post); + return View(model); + } catch (InvalidOperationException e) { + return NotFound(); + } } } } diff --git a/Website/Startup.cs b/Website/Startup.cs index 79d7a9b..6d6adf4 100644 --- a/Website/Startup.cs +++ b/Website/Startup.cs @@ -40,18 +40,18 @@ namespace Website // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { - if (env.IsDevelopment()) + if (env.IsDevelopment() ) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error/ServerError"); - app.UseStatusCodePagesWithReExecute("/Error/PageNotFound"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } + app.UseStatusCodePagesWithReExecute("/Error/PageNotFound"); app.UseHttpsRedirection(); app.UseStaticFiles();