HTML page Generation aspx page

Source: Internet
Author: User
Tags httpcontext

This feature is a small feature that is deducted from the Dtcms demo website, which can be accessed by writing all the code in HTML and then generating the ASPX page. The specific process of the next article to analyze. This article is mainly about how HTML generates ASPX pages.

Principle:

We first read the HTML page and then stitch the string (the string that makes up the structure of the ASPX page ), and then create the ASPX page to write the string into the ASPX page using the file operation.

For example:

The structure of the ASPX page that we are going to generate is roughly the following (the simplest, no interaction involving data, just a static string )

Static page:

Generate the ASPX page code as follows

      //static pages that need to generate ASPX pages                stringTempletfullpath = HttpContext.Current.Server.MapPath ("/html/test.html"); //reading static files                using(StreamReader objreader =NewStreamReader (Templetfullpath, Encoding.UTF8)) {StringBuilder Textoutput=NewStringBuilder (70000);  Textoutput.append (Objreader.readtoend ()); //read the file into the StringBuilder                    stringOldstr =textoutput.tostring (); stringNewstr =""; Newstr=Oldstr;
Focus
//Replace Special tagsNewstr = Newstr.replace ("\ r \ n","\\r\\n");
Newstr= Newstr.replace ("\"","\\\"");
           //We write </script> the whole to tempplatebulder inside can not display correctly, need to split, write to two append () inside newstr= Newstr.replace ("</script>","</\ "); \r\n\ttemplatebuilder.append (\" Script>");
StringBuilder Sbstr=NewStringBuilder (); //Add page Header Inherits The value of this property must correspond to the correctSbstr.appendformat ("<%@ page Language =\ "c#\" autoeventwireup=\ "true\" inherits=\ "{0}\" validaterequest=\ " false\"%>\r\n","dtcmstest.test"); //namespaces are added to the namespace according to the requirements of the page, or they can be referenced in the corresponding *.cs file .Sbstr.append ("<%@ Import namespace=\ "system.text\"%>\r\n"); //add scripts that can write background codeSbstr.append ("<script runat =\ "server\" >\r\nprotected override void OnInit (EventArgs e) \ r \ n"); Sbstr.append ("{\r\n\r base. OnInit (e); \r\n\t");

//Basically this piece of code is dynamic, the rest is the same //use the StringBuilder output page to first read all the HTML into the StringBuilder and then outputSbstr.appendformat ("StringBuilder templatebuilder = new StringBuilder ({0}); \r\n\t", textoutput.capacity); Sbstr.appendformat ("templatebuilder.append (\ "{0}\"); \r\n\t", NEWSTR);

Sbstr.append ("Response.Write (Templatebuilder.tostring ()); \ r \ n}\r\n"); Sbstr.append ("</script>\r\n");
stringPagedir = HttpContext.Current.Server.MapPath ("/html"); stringOutputPath = Pagedir +"\\Test.aspx";//the physical path of the generated file//created if the physical path does not exist if(!directory.exists (Pagedir)) {directory.createdirectory (Pagedir); }
          //Focus //Save Write fileFile.writealltext (OutputPath, sbstr.tostring (), Encoding.UTF8); Context. RewritePath ("/html/test.aspx");

To generate a string note for an ASPX page:

We use file to create an ASPX page and then go into the write data, and it will recognize some special tokens.

Gca

We write some strings with special symbols, write files using file

We need to output the HTML page data from the beginning to get in the background code, not destroy, so we need to replace

If we need to write the backend code, we create a new *.cs file with the same name as the ASPX page, and then let the class inherit the page.

This is all about the principle, no data interaction involved.

HTML page Generation aspx page

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.