Display the custom error page in ASP. NET Core, asp. netcore

Source: Internet
Author: User

Display the custom error page in ASP. NET Core, asp. netcore

Preface

I believe that every programmer should know that in ASP. NET Core, by default, only the http status code is returned when a 500 or 404 error occurs, and no content is returned. The page is blank.

If you addapp.UseStatusCodePages(); , 500 error is still blank (I do not know why it does not work for 500 error), 404 error changes, the page will display the following text:

Status Code: 404; Not Found 

What should we do if we want to display custom friendly error pages regardless of 500 or 404 errors?

For Error 500, we can useapp.UseExceptionHandler() Interception;

For Error 404, we can useapp.UseStatusCodePages() Enhanced versionapp.UseStatusCodePagesWithReExecute() Interception;

Then it is forwarded to the corresponding URL for processing.

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

The URL is routed to the MVC Controller to display 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 I found a problem. When an underlying exception occurs, the custom error page cannot be displayed, but it is still blank, for example, 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 time, I think of the limitations of Displaying Custom error pages with MVC. If an exception occurs and the MVC itself cannot work properly, the custom error page cannot be displayed.

Therefore, we made improvements to this problem. For Error 500, we directly responded using static files, and Startup. cs's Configure() The code in 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 the custom error page, the following changes have been made in the 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:500)}.html";  return new PhysicalFileResult(filePath, new MediaTypeHeaderValue("text/html")); }  }

Summary

The above is about ASP. NET Core displays all the content of the custom error page. I hope the content in this article will help you in your study or work. If you have any questions, please leave a message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.