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

来源:互联网 发布:同城配送软件 编辑:程序博客网 时间:2024/05/22 11:16

两个比较重点的知识:

1.使用页适配器:http://www.cnblogs.com/sifang2004/archive/2006/05/31/414182.html

如果用新建一个pageBase:Page 来给所有页面继承,程序改动非常大,还有可能改少了。使用页适配器刚好可以达到可插拔的效果,功能又一样。

2.重写Render:http://www.cnblogs.com/mxw09/archive/2010/12/15/1906783.html

链接里的文章说的很清楚了,使用传统的法写会导致输出以后html内容顺序错乱,比如:Substitution控件,它生成的HTML总是跑到HTML最上面


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

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

<browser refID="Default">
    <controlAdapters>
      <adapter controlType="System.Web.UI.Page"
               adapterType="TestWeb.MyPageAdapter,TestWeb" />
    </controlAdapters>
  </browser>

代码如下:

[csharp] view plain copy
  1. namespace TestWeb{  
  2. public class MyPageAdapter : System.Web.UI.Adapters.PageAdapter  
  3.     {  
  4.         public string GetRenderHtml(HtmlTextWriter writer)  
  5.         {  
  6.             var Response = this.Page.Response;  
  7.   
  8.             //Response.Output其实就是一个HttpWriter,Response.OutputStream其实就是HttpResponseStream,Object.ReferenceEquals (Response.Output,(Response.OutputStream as HttpResponseStream)._writer)为true  
  9.   
  10.             BindingFlags bind = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField;  
  11.             //因为HttpWriter._charBuffer这个字符数组的长度是1024,  
  12.             //所以推测,一旦某一段字符串超过1024就会创建一个IHttpResponseElement,  
  13.             //然后加入到HttpWriter._buffers,HttpWriter._buffers的每一个元素都实现了IHttpResponseElement接口,  
  14.             //具体类型可能是HttpResponseUnmanagedBufferElement,HttpSubstBlockResponseElement等类型  
  15.             ArrayList arr = (ArrayList)Response.Output.GetType().GetField("_buffers", bind).GetValue(Response.Output);  
  16.   
  17.             Assembly systemWeb = Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");  
  18.             Type type = systemWeb.GetType("System.Web.IHttpResponseElement");  
  19.             MethodInfo method = type.GetMethod("GetBytes");  
  20.             StringBuilder sb = new StringBuilder(5000);  
  21.             //遍历每一个buffer,获取buffer里存储的字符数组,然后转换为字符串  
  22.             for (int i = 0; i < arr.Count; i++)  
  23.             {  
  24.                 byte[] buffer = (byte[])method.Invoke(arr[i], null);  
  25.                 //使用当前编码得出已经存储到HttpWriter._buffers中的字符串  
  26.                 sb.Append(Response.ContentEncoding.GetString(buffer));  
  27.             }  
  28.             //获取HttpWriter的字符数组缓冲区  
  29.             char[] charBuffer = (char[])Response.Output.GetType().GetField("_charBuffer", bind).GetValue(Response.Output);  
  30.             int charBufferLength = (int)Response.Output.GetType().GetField("_charBufferLength", bind).GetValue(Response.Output);  
  31.             int charBufferFree = (int)Response.Output.GetType().GetField("_charBufferFree", bind).GetValue(Response.Output);  
  32.             //charBufferLength - charBufferFree 等于字符数组缓冲区已经使用的字符数量  
  33.             for (int i = 0; i < charBufferLength - charBufferFree; i++)  
  34.             {  
  35.                 sb.Append(charBuffer[i]);  
  36.             }  
  37.             string html = sb.ToString();  
  38.             return html;  
  39.         }  
  40.         protected override void Render(HtmlTextWriter writer)  
  41.         {  
  42.             base.Render(writer);  
  43.             string html = GetRenderHtml(writer);  
  44.             html = GetNewImageUrl(html);  
  45.             this.Page.Response.ClearContent();  
  46.             this.Page.Response.Write(html);  
  47.         }  
  48.         public string GetNewImageUrl(string html)  
  49.         {  
  50.             string reg = "\"/publicimg/[^\"\"]*\"|\'/publicimg/[^\'\']*\'";  
  51.             reg += "|\\(/publicimg/[^\\(\\)]*\\)";  
  52.             return Regex.Replace(html, reg  
  53.                 , new MatchEvaluator(GetNewImageUrlTag),  
  54.                                             RegexOptions.IgnoreCase  
  55.                                           | RegexOptions.Multiline  
  56.                                           | RegexOptions.IgnorePatternWhitespace  
  57.                                           | RegexOptions.Compiled);  
  58.         }  
  59.         private string GetNewImageUrlTag(Match match)  
  60.         {  
  61.             string quotesStart = match.Groups[0].Value[0].ToString();  
  62.             string quotesEnd = match.Groups[0].Value[match.Groups[0].Value.Length - 1].ToString();  
  63.             string imgName = match.Groups[0].Value.ToLower().Replace(quotesStart, "").Replace(quotesEnd, "");  
  64.             if (!string.IsNullOrEmpty(imgName) && imgName.StartsWith("/publicimg/"))  
  65.             {  
  66.                 imgName = "http://loaclhost:17100/"+imgName;  
  67.             }  
  68.             return quotesStart + imgName + quotesEnd;  
  69.         }  
  70.          
  71.     }  
阅读全文
0 0
原创粉丝点击