I am trying to implement a custom 404 page in my .NET 5.0 for when the web app is in production. I have implemented the following in Startup.cs;
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseStatusCodePagesWithRedirects("/Error/{0}");
}
...
}
Linking to a simple ErrorController
public class ErrorController : Controller
{
[Route("/Error/{statusCode}")]
public IActionResult HttpStatusCodeHandler(int statusCode)
{
switch(statusCode)
{
case 404:
//ViewBag.ErrorMessage("Resource could not be found.");
break;
}
return View("Error");
}
}
and goes to Error.cshtml found in Shared.
This does nothing to remove the default Status Code: 404; Not Found
page, but the page is accessible if I go to localhost/Error/404
directly through url.
This is how I remember implementing this with previous versions of .NET, but now I'm unsure if I'm missing something or if the new .NET 5.0 has a new requirement for implementing the custom 404 page.
Any help would be greatly appreciated.
Edit: launchSettings.json Profile:
"Proj_prod": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Production"
}
}
"ASPNETCORE_ENVIRONMENT"
is set to"Production"
. Edited the question to include the launchSettings profile details just in case. – DuweIIS Express
toProduction
and using your code will work well. – Vervain