当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net core使用中间件美化开发环境异常页面

asp.net core使用中间件美化开发环境异常页面

2019年06月19日  | 移动技术网IT编程  | 我要评论

hp梅洛普冈特将军,迪兰恒进官网,让令狐潮开口

asp.net core系统自带的异常页面色彩给人感觉模糊、朦胧,晕眩!

 

原版:

 

美化版

 

 

实现思路:(在系统自带异常中间件“developerexceptionpagemiddleware”执行后,调用自定义的异常中间件“developerexceptionprettifymiddleware”,继续向响应流输出美化的css和js)

/// <summary>
/// 开发环境异常页面css美化 中间件
/// </summary>
public class developerexceptionprettifymiddleware
{
    private readonly requestdelegate _next;

    public developerexceptionprettifymiddleware(
        requestdelegate next)
    {
        _next = next;
    }

    public async task invoke(httpcontext context)
    {
        await _next.invoke(context);

        if (context.response.statuscode == 500) // 通过 statuscode 判断程序报错
        {
            using (textwriter output = (textwriter)new streamwriter(context.response.body,
                new utf8encoding(false, true), 4096, true))
            {
                // 美化版 css/js
                var chars = @"
                    <style>
                        body{ color: inherit}
                        h1{color:red}
                        h3{color:inherit}
                        .titleerror{color:maroon}
                        body .location{ }
                        #header li{color:blue}
                        #header .selected{background:#44525e}
                        #stackpage .source ol li{background-color:#ffffcc}
                        #stackpage .source ol.collapsible li span{color:#000}
                        .rawexceptionstacktrace{background-color:#ffffcc; padding:.5rem}
                        :focus{outline:none}
                        .showrawexception{color:blue}
                    </style>
                    <script>
                        document.queryselector('.expandcollapsebutton').click()
                    </script>
            ".tochararray();

                // 输出到响应流中
                await output.writeasync(chars, 0, chars.length);
                await output.flushasync();
            }
        }
    }
}

 

使用中间件(注意顺序)

源码下载

https://github.com/246850/aspnetcore.prettify/

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网