Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Error Page in Production for .NET 5.0

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"
            }
        }
like image 751
UgKnu Avatar asked May 29 '21 15:05

UgKnu


People also ask

How can create custom error page in ASP.NET Core?

Serve error pages in ASP.NET Core application. Starting from an empty project, created in a previous post, amend the Configure() method of Startup class to use middleware needed for error handling. Below I've used a custom middleware (defined as lambda) to handle production exceptions, public void Configure(

What must be used to configure the custom error handling page in the production environment?

To configure a custom error handling page for the Production environment, call UseExceptionHandler. This exception handling middleware: Catches and logs unhandled exceptions. Re-executes the request in an alternate pipeline using the path indicated.

How do I display an error page?

To display a custom error page with an appropriate error code, use the <httpErrors> section only, and do not use the <customErrors> section. Add the following <httpErrors> section under <system. webServer> section, as shown below.


Video Answer


1 Answers

Okay the actual code in question works as intended. A work partner added app.UseStatusCodePages(); far later down the Configure which I didn't notice.

(Yes, it took 14 days. I've finally went back to this and just noticed.)

like image 82
UgKnu Avatar answered Oct 24 '22 00:10

UgKnu