Problem
How to create error pages for specific HTTP status codes in ASP.NET Core.
Solution
Create an empty project and update Startup to add middleware for Status Code Pages.
- public class Startup
- {
- public void ConfigureServices(
- IServiceCollection services)
- {
- services.AddMvc();
- }
- public void Configure(
- IApplicationBuilder app,
- IHostingEnvironment env)
- {
- app.UseStatusCodePagesWithReExecute("/Errors/Index", "?statusCode={0}");
- app.UseMvcWithDefaultRoute();
- }
- }
Note
The {0} is substituted with HTTP status code that triggered the execution middleware.
Add Errors Controller to catch the status code errors.
- public class ErrorsController : Controller
- {
- public IActionResult Index(int statusCode)
- {
- var feature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
- ViewBag.StatusCode = statusCode;
- ViewBag.OriginalPath = feature?.OriginalPath;
- ViewBag.OriginalQueryString = feature?.OriginalQueryString;
- return View();
- }
- }
Add a view for Index View and display error details.
- <p>Status Code: <b>@ViewBag.StatusCode</b></p>
- <p>OriginalPath: <b>@ViewBag.OriginalPath</b></p>
- <p>OriginalQueryString: <b>@ViewBag.OriginalQueryString</b></p>
Add Home controller to return the status codes.
- public class HomeController : Controller
- {
- public IActionResult Index() => View();
- public IActionResult Throw404(int id) => StatusCode(404);
- public IActionResult Throw412(int id) => StatusCode(412);
- public IActionResult Throw500(int id) => StatusCode(500);
- }
Add a view for Index View.
- <p>You've reached Home</p>
- <ul>
- <li><a href="/Home/Throw404?id=1">404 (Not Foud)</a></li>
- <li><a href="/Home/Throw412?id=2">412 (Precondition Failed)</a></li>
- <li><a href="/Home/Throw500?id=3">500 (Internal Server Error)</a></li>
- </ul>
Run and click one of the links. You’ll get the view from Errors controller. Do note, however, that the URL doesn’t change.



Join the conversation! Your thoughts help the community grow.