Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of displaying custom error pages in ASP.NETCore

Shulou Source: shulou.com Published: 2022-06-03 13:09:42 11月14日 Update

This article shows you how to solve the problem of displaying custom error pages in ASP.NETCore, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

I believe every programmer should know that in ASP.NET Core, by default, when 500or 404 errors occur, only the http status code is returned, nothing is returned, and the page is blank.

If you add app.UseStatusCodePages () to the Configure () of Startup.cs, the page will display the following text if it is still blank at 500th error (somehow it doesn't work for 500error), and it will change with 404error:

Status Code: 404; Not Found

What if we want to display our own customized friendly error page regardless of 500 or 404 errors?

For 500errors, we can intercept them with app.UseExceptionHandler ()

For 404 errors, we can intercept them with app.UseStatusCodePagesWithReExecute (), the enhanced version of app.UseStatusCodePages ().

It is then handed over to the corresponding URL for processing.

App.UseExceptionHandler ("/ errors/500"); app.UseStatusCodePagesWithReExecute ("/ errors/ {0}")

The URL route to MVC Controller displays a friendly error page.

Public class ErrorsController: Controller {[Route ("errors/ {statusCode}")] public IActionResult CustomError (int statusCode) {if (statusCode = = 404) {return View ("~ / Views/Errors/404.cshtml");} return View ("~ / Views/Errors/500.cshtml");}}

[update]

Later, a problem was found that when an underlying exception occurred, the custom error page could not be displayed, or went blank, such as the following exception:

System.DllNotFoundException: Unable to load DLL 'System.Security.Cryptography.Native.Apple': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

At this point, I think of the limitations of using MVC to display custom error pages. If an exception occurs so that MVC itself does not work properly, the custom error page cannot be displayed.

So an improvement is made to solve this problem, and the code in Configure () of Startup.cs is as follows:

App.UseExceptionHandler (errorApp = > {errorApp.Run (async context = > {context.Response.StatusCode = 500; if (context.Request.Headers ["X-Requested-With"]! = "XMLHttpRequest") {context.Response.ContentType = "text/html"; await context.Response.SendFileAsync ($@ "{env.WebRootPath} / errors/500.html");});}); app.UseStatusCodePagesWithReExecute ("/ errors/ {0}")

To reuse custom error pages, modifications have been made in MVC Controller:

Public class ErrorsController: Controller {private IHostingEnvironment _ env; public ErrorsController (IHostingEnvironment env) {_ env = env;} [Route ("errors/ {statusCode}")] public IActionResult CustomError (int statusCode) {var filePath = $"{_ env.WebRootPath} / errors/ {(statusCode = = 404) 404)} .html"; return new PhysicalFileResult (filePath, new MediaTypeHeaderValue ("text/html")) }} the above content is how to solve the problem of displaying custom error pages in ASP.NETCore. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Errors pages content blanks skills knowledge or problems faceted concise concise code function that is the bottom layer what to do the situation files text articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Information Apple vpn Microsoft