ASP. NET uses page adapters and overrides render to modify the HTML content of the full-station output page without confusion

Source: Internet
Author: User

Two more important points of knowledge:

1. Using the page adapter: http://www.cnblogs.com/sifang2004/archive/2006/05/31/414182.html

If you use a new pagebase:page to inherit all pages, the program changes very much, and it is possible to change them less. Using the page adapter just can achieve the pluggable effect, the same function.

2. Rewrite render:http://www.cnblogs.com/mxw09/archive/2010/12/15/1906783.html

The article in the link is very clear, the use of traditional notation will result in the output of HTML content after the order of confusion, such as: substitution control, it generated HTML always run to the top of the HTML


Function: Append the HTML of the output of the site to the string preceded by publicimg with http://loaclhost:17100

Page Adapter Configuration grammar: 1. Add a new App_Browsers folder to the Web project, add a Default.browser file, and add a new one to the <browsers> node

<browser refid= "Default" >
<controlAdapters>
<adapter controltype= "System.Web.UI.Page"
Adaptertype= "testweb.mypageadapter,testweb"/>
</controlAdapters>
</browser>

The code is as follows:

Namespace Testweb{public class MyPageAdapter:System.Web.UI.Adapters.PageAdapter {public string getrenderhtml ( HtmlTextWriter writer) {var Response = this.            Page.Response; Response.Output is actually a httpwriter,response.outputstream is actually httpresponsestream,object.referenceequals ( Response.Output, (Response.outputstream as Httpresponsestream). _writer) True bindingflags bind = bindingflags.no Npublic | BindingFlags.Instance | Bindingflags.ignorecase |            Bindingflags.getfield; Because httpwriter._charbuffer the length of this character array is 1024,//So presumably, once a string of more than 1024 will create a ihttpresponseelement,//And then add to the HT Each element of the Tpwriter._buffers,httpwriter._buffers implements the Ihttpresponseelement interface,//the specific type may be Httpresponseunmanagedbufferelem Ent,httpsubstblockresponseelement and other types 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); Iterate through each buffer, get an array of characters stored in buffer, and convert to a string for (int i = 0; i < arr. Count; i++) {byte[] buffer = (byte[]) method.                Invoke (Arr[i], NULL); Use the current encoding to derive the string SB that has been stored in the httpwriter._buffers.            Append (Response.ContentEncoding.GetString (buffer)); }//Get Httpwriter character array 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); CharbufferleNgth-charbufferfree equals the number of characters that the character array buffer has used 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://loaclho            St:17100/"+imgname;        } return Quotesstart + Imgname + quotesend; }           }}


ASP. NET uses page adapters and overrides render to modify the HTML content of the full-station output page without confusion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.