Learn Net Core 2.0 make a blog error page middleware

Source: Internet
Author: User

Customerrorpagesmiddleware

 Public classCustomerrorpagesmiddleware {Private ReadOnlyrequestdelegate _next; Private ReadOnlyMicrosoft.Extensions.Logging.ILogger _logger; Private ReadOnlyihostingenvironment _env;  PublicCustomerrorpagesmiddleware (requestdelegate Next, Iloggerfactory loggerfactory, Ihos Tingenvironment env) {_next=Next; _logger= loggerfactory.createlogger<customerrorpagesmiddleware>(); _env=env; }         Public AsyncTask Invoke (HttpContext context) {Try            {                await_next (context); }            Catch(Exception ex) {_logger. LogError (0, ex,"An unhandled exception have occurred while executing the request"); if(context. response.hasstarted) {_logger. Logwarning ("The response has already started, the error page middleware would not be executed."); Throw; }                Try{context.                    Response.Clear (); Context. Response.statuscode= -; return; }                Catch(Exception ex2) {_logger. LogError (0, EX2,"An exception is thrown attempting to display the error page."); }                Throw; }            finally            {                varStatusCode =context.                Response.statuscode; if(StatusCode = =404|| StatusCode = = -|| StatusCode = =403)                {                                      awaitErrorpage.responseasync (context.                Response, statuscode,_env); }                            }        }    }

ErrorPage

  Public Static classErrorPage { Public Static AsyncTask Responseasync (HttpResponse response,intStatusCode, Ihostingenvironment env) {            if(StatusCode = =404)            {                                  awaitResponse.                       WriteAsync (Page404,encoding.utf8); }            Else if(StatusCode = = -)            {                awaitResponse.            WriteAsync (Page500); }Else if(StatusCode = =403{ //No permissions server will return 403;                 awaitResponse. WriteAsync (Page403); }        }        Private Static stringPage404 ="404"; Private Static stringPage500 =""; Private Static stringPage403 ="403"; }

Customerrorpagesextensions

 Public Static class customerrorpagesextensions    {        publicstatic iapplicationbuilder usecustomerrorpages ( This Iapplicationbuilder app)            {ifnull)            {                throw New ArgumentNullException (nameof (APP));            }             return app. Usemiddleware<customerrorpagesmiddleware>();        }    }

Startup

App. Usecustomerrorpages ();

Learn Net Core 2.0 make a blog error page middleware

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.