欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 使用页适配器和重写Render对全站输出的页面的HTML内容进行修改,不会错乱

程序员文章站 2022-07-05 22:56:53
    功能:把站点输出的HTML以publicimg开头的字符串前面加上https://loaclhost:17100 页适配器配置文法:1.在WEB项目里新...

 

 

功能:把站点输出的HTML以publicimg开头的字符串前面加上https://loaclhost:17100

页适配器配置文法:1.在WEB项目里新增App_Browsers文件夹,新增一个Default.browser文件,在节点里新增



adapterType=TestWeb.MyPageAdapter,TestWeb />

 

代码如下:

 

namespace TestWeb{
public class MyPageAdapter : System.Web.UI.Adapters.PageAdapter
    {
        public string GetRenderHtml(HtmlTextWriter writer)
        {
            var Response = this.Page.Response;

            //Response.Output其实就是一个HttpWriter,Response.OutputStream其实就是HttpResponseStream,Object.ReferenceEquals (Response.Output,(Response.OutputStream as HttpResponseStream)._writer)为true

            BindingFlags bind = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField;
            //因为HttpWriter._charBuffer这个字符数组的长度是1024,
            //所以推测,一旦某一段字符串超过1024就会创建一个IHttpResponseElement,
            //然后加入到HttpWriter._buffers,HttpWriter._buffers的每一个元素都实现了IHttpResponseElement接口,
            //具体类型可能是HttpResponseUnmanagedBufferElement,HttpSubstBlockResponseElement等类型
            ArrayList arr = (ArrayList)Response.Output.GetType().GetField(_buffers, bind).GetValue(Response.Output);

            Assembly systemWeb = Assembly.Load(System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a);
            Type type = systemWeb.GetType(System.Web.IHttpResponseElement);
            MethodInfo method = type.GetMethod(GetBytes);
            StringBuilder sb = new StringBuilder(5000);
            //遍历每一个buffer,获取buffer里存储的字符数组,然后转换为字符串
            for (int i = 0; i < arr.Count; i++)
            {
                byte[] buffer = (byte[])method.Invoke(arr[i], null);
                //使用当前编码得出已经存储到HttpWriter._buffers中的字符串
                sb.Append(Response.ContentEncoding.GetString(buffer));
            }
            //获取HttpWriter的字符数组缓冲区
            char[] charBuffer = (char[])Response.Output.GetType().GetField(_charBuffer, bind).GetValue(Response.Output);
            int charBufferLength = (int)Response.Output.GetType().GetField(_charBufferLength, bind).GetValue(Response.Output);
            int charBufferFree = (int)Response.Output.GetType().GetField(_charBufferFree, bind).GetValue(Response.Output);
            //charBufferLength - charBufferFree 等于字符数组缓冲区已经使用的字符数量
            for (int i = 0; i < charBufferLength - charBufferFree; i++)
            {
                sb.Append(charBuffer[i]);
            }
            string html = sb.ToString();
            return html;
        }
        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            string html = GetRenderHtml(writer);
            html = GetNewImageUrl(html);
            this.Page.Response.ClearContent();
            this.Page.Response.Write(html);
        }
        public string GetNewImageUrl(string html)
        {
            string reg = /publicimg/[^\]*|'/publicimg/[^'']*';
            reg += |\(/publicimg/[^\(\)]*\);
            return Regex.Replace(html, reg
                , new MatchEvaluator(GetNewImageUrlTag),
                                            RegexOptions.IgnoreCase
                                          | RegexOptions.Multiline
                                          | RegexOptions.IgnorePatternWhitespace
                                          | RegexOptions.Compiled);
        }
        private string GetNewImageUrlTag(Match match)
        {
            string quotesStart = match.Groups[0].Value[0].ToString();
            string quotesEnd = match.Groups[0].Value[match.Groups[0].Value.Length - 1].ToString();
            string imgName = match.Groups[0].Value.ToLower().Replace(quotesStart, ).Replace(quotesEnd, );
            if (!string.IsNullOrEmpty(imgName) && imgName.StartsWith(/publicimg/))
            {
                imgName = https://loaclhost:17100/+imgName;
            }
            return quotesStart + imgName + quotesEnd;
        }
       
    }
}