標籤:asp.net html pageadapter 重寫 render
兩個比較重點的知識:
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; } }}
asp.net 使用頁適配器和重寫Render對全站輸出的頁面的HTML內容進行修改,不會錯亂