NETCORE implementation of 404 and 500 status code custom processing page

Time:2021-5-13

In the process of using nercore to develop the framework, it is necessary to process the friendly prompt page of 404500 and other status codes. Referring to asp.net MVC, no application is provided_ Error and Application_ Beginrequest method will not intercept when the interceptor route does not match. However, NETCORE provides the use extension method in microsoft.aspnetcore.builder.useextensions to intercept httpcontext. In this way, we can obtain the request and response to process the jump. We add the following code to the configure method of startup, In the case of 404, you can also handle the default route jump when accessing items, such as accesshttp://localhost:4099/fastcloudIf the specific main page route is not added, the context.request.path is empty, and we can jump to the default main page

//Custom 404 and 500 processing
                app.Use(async (context, next) =>
                {
                    await next();
                    if (context.Response.StatusCode == 404)
                    {
                        string FilePath = context.Request.Path;
                        if (string.IsNullOrEmpty(FilePath) || FilePath == "/")
                        {
                            context.Request.Path = "/" + AppConfigUtil.Configuration["Frame:DefaultHomeUrl"];
                        }
                        else
                        {
                            context.Request.Path = "/frame/home/error/404";
                        }
                        await next();
                    }
                    if (context.Response.StatusCode == 500)
                    {
                        context.Request.Path = "/frame/home/error/500";
                    }
                });

It should be noted that if the global exception interceptor is added to the project, you need to judge that if it is a page request, you will jump to the custom 500 page. The Ajax request will return the wrong JSON string. The specific code and effect are as follows

public class GlobalExceptionFilter : IExceptionFilter
    {
        public void OnException(ExceptionContext context)
        {
            string FloderPath = CloudUtil.GetContentPath() + "/Logs";
            DirectoryInfo SystemLogDir = new DirectoryInfo(FloderPath);
            if (!SystemLogDir.Exists)
            {
                SystemLogDir.Create();
            }
            StringBuilder builder = new StringBuilder();
            Builder. Appendformat ("exception request URL: {0}", context. Httpcontext. Request. Path + environment. Newline) ";
            Builder. Appendformat ("exception information: {0}", context. Exception. Message + environment. Newline) ";
            Builder. Appendformat ("stack information: {0}", context. Exception. Stacktrace + environment. Newline) ";
            LogUtil.WriteLog(CloudUtil.GetContentPath() + "/Logs/Exception", "log_", builder.ToString());
            bool IsAjaxCall = context.HttpContext.Request.Headers["x-requested-with"] == "XMLHttpRequest";
            if (IsAjaxCall)
            {
                Context. Result = result. Error ("system error, please contact administrator!");
                context.ExceptionHandled = true;
            }
            else
            {
                context.Result = new RedirectResult(CloudUtil.GetRootPath() + "frame/home/error/500");
                context.ExceptionHandled = true;
            }

        }