示例#1
0
        public IActionResult Index()
        {
            var exceptionHandlerPathFeature = HttpContext.Features.Get <IExceptionHandlerPathFeature>();
            var ex            = exceptionHandlerPathFeature?.Error;
            var knowException = ex as IKnowException;

            if (knowException == null)
            {
                var logger = HttpContext.RequestServices.GetService <ILogger <MyExceptionFilterAttribute> >();
                logger.LogError(ex, ex.Message);
                knowException = KnowException.Unknown;
            }
            else
            {
                knowException = KnowException.FromKnownException(knowException);
            }
            return(View(knowException));
        }
示例#2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //if (env.IsDevelopment())
            //{
            //    app.UseDeveloperExceptionPage();
            //}
            //app.UseExceptionHandler("/error");
            app.UseExceptionHandler(configure =>
            {
                configure.Run(async context =>
                {
                    IExceptionHandlerPathFeature exceptionHandlerPathFeature = context.Features.Get <IExceptionHandlerPathFeature>();
                    Exception ex = exceptionHandlerPathFeature?.Error;

                    var knowException = ex as IKnowException;
                    if (knowException == null)
                    {
                        knowException = KnowException.Unkown;
                        context.Response.StatusCode = StatusCodes.Status500InternalServerError;
                    }
                    else
                    {
                        knowException = KnowException.FromKnowException(knowException);
                        context.Response.StatusCode = StatusCodes.Status200OK;
                    }
                    var jsonOptions = context.RequestServices.GetService <IOptions <JsonOptions> >();
                    context.Response.ContentType = "application/json;charset=utf-8";
                    await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(knowException, jsonOptions.Value.JsonSerializerOptions));
                });
            });

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }