Refactor error handling. Add 404 page
Questo commit è contenuto in:
parent
2475d1963c
commit
647696aa92
5 ha cambiato i file con 38 aggiunte e 17 eliminazioni
27
Website/Controllers/ErrorController.cs
File normale
27
Website/Controllers/ErrorController.cs
File normale
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Website.ViewModels;
|
||||
|
||||
namespace Website.Controllers
|
||||
{
|
||||
public class ErrorController : Controller
|
||||
{
|
||||
|
||||
public IActionResult BlowUp() {
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult ServerError()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult PageNotFound()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,7 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Website.Data;
|
||||
|
@ -23,11 +20,5 @@ namespace Website.Controllers
|
|||
var post = await _blogRepo.GetLatestPostAsync();
|
||||
return View(post);
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.HttpsPolicy;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
@ -51,7 +46,8 @@ namespace Website
|
|||
}
|
||||
else
|
||||
{
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
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();
|
||||
}
|
||||
|
|
7
Website/Views/Error/PageNotFound.cshtml
File normale
7
Website/Views/Error/PageNotFound.cshtml
File normale
|
@ -0,0 +1,7 @@
|
|||
@model ErrorViewModel
|
||||
@{
|
||||
ViewData["Title"] = "404 Not Found";
|
||||
}
|
||||
|
||||
<h1>404 Not Found - page unavailable.</h1>
|
||||
This page cannot be found.
|
Caricamento…
Aggiungi tabella
Aggiungi collegamento
Crea riferimento in una nuova segnalazione