两个比较重点的知识:
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>
代码如下:
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 = "http://loaclhost:17100/"+imgName; } return quotesStart + imgName + quotesEnd; } } }
时间: 2024-10-10 00:50:13