If you have a standard ASP.NET 5 web application template project, you can get a really nice solution that will treat as saving the error status code at the same time as maintaining the cshtml file of your choice.
Startup.cs , in the Configure method
Replace
app.UseExceptionHandler("/Error");
from
app.UseStatusCodePagesWithReExecute("/Home/Errors/{0}");
Then remove the following command:
app.Run(async (context) => { var logger = loggerFactory.CreateLogger("Catchall Endpoint"); logger.LogInformation("No endpoint found for request {path}", context.Request.Path); await context.Response.WriteAsync("No endpoint found - try /api/todo."); });
In HomeController.cs
Replace the current Error method as follows:
public IActionResult Errors(string id) { if (id == "500" | id == "404") { return View($"~/Views/Home/Error/{id}.cshtml"); } return View("~/Views/Shared/Error.cshtml"); }
This way you can add error files that you think users can get (while search engines still get the correct status codes)
Now add a folder called Error to the ~Views/Home folder. Create two .cshtml files. Name the first 404.cshtml and the second 500.cshtml .
Give them useful content and run the application and write something like http://localhost:44306/idonthaveanendpointhere and confirm that the installation works.
Also click on the F12 developer tools and confirm that the status code is 404.
Magnus Karlsson Feb 12 '16 at 14:12 2016-02-12 14:12
source share