Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report