两个比较重点的知识:
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; BindingFlags bind = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField; 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); for ( int i = 0; i < arr.Count; i++) { byte [] buffer = ( byte [])method.Invoke(arr[i], null ); sb.Append(Response.ContentEncoding.GetString(buffer)); } 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); 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; } } }