Beispiel #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseExceptionHandler(appBuilder =>
            {
                appBuilder.Run(async context =>
                {
                    //从http请求中获取Exception异常对象
                    var exceptionHandlerPathFeature = context.Features.Get <IExceptionHandlerPathFeature>();
                    Exception ex = exceptionHandlerPathFeature?.Error;

                    var knownException = ex as IKnownException;

                    if (knownException != null)//实现了IKnownException接口,即是我们自定义的异常
                    {
                        knownException = KnownException.Build(knownException);
                        context.Response.StatusCode = StatusCodes.Status200OK;
                    }
                    else//未实现IKnownException接口,即不是我们自定义的已知异常
                    {
                        var logger = context.RequestServices.GetService <ILogger <Startup03> >();
                        logger.LogError(exceptionHandlerPathFeature?.Error, exceptionHandlerPathFeature?.Error.Message);

                        knownException = KnownException.Unknown;
                        context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                    }

                    var jsonOptions = context.RequestServices.GetService <IOptions <JsonOptions> >();
                    context.Response.ContentType = "application/json; charset=utf-8";
                    await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(knownException, jsonOptions.Value.JsonSerializerOptions));
                });
            });

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
        public IActionResult Index()
        {
            //从http请求中获取Exception异常对象
            var       exceptionHandlerPathFeature = HttpContext.Features.Get <IExceptionHandlerPathFeature>();
            Exception ex = exceptionHandlerPathFeature?.Error;

            var knownException = ex as IKnownException;

            if (knownException != null)//实现了IKnownException接口,即是我们自定义的异常
            {
                knownException = KnownException.Build(knownException);
            }
            else//未实现IKnownException接口,即不是我们自定义的已知异常
            {
                var logger = HttpContext.RequestServices.GetService <ILogger <ErrorController> >();
                logger.LogError(ex, ex?.Message);
                knownException = KnownException.Unknown;
            }

            return(View(knownException));
        }