ASP. NET generates static HTML based on existing dynamic page

Source: Internet
Author: User

The format of the existing dynamic page is similar to the pagename.aspx?id=1 format, and later, due to the publisher, the dynamic page is required to be converted to static HTML after uploading.

First, according to the format generated by the page, enumeration gets the page HTML:

1             foreach(varPageinchpagelist)2             {3                 stringhtml = readhtml (string. Format ("pagename.aspx?id={0}",;4HTML =replacelistandsingle (HTML);5WriteHTML (string. Format ("pagename_{0}.html",, HTML);6}

To read an ASP page:

         Public Static stringReadhtml (stringsourceURL) {            Try{System.Net.WebRequest myrequest=System.Net.WebRequest.Create (sourceURL); System.Net.WebResponse Myresponse=Myrequest.getresponse (); using(Stream stream =Myresponse.getresponsestream ()) {                    using(StreamReader sr =NewStreamReader (Stream, encoding.getencoding ("Utf-8")))                    {                        returnSr.                    ReadToEnd (); }                }            }            Catch(Exception ex) {Throw NewException (string. Format ("readhtml Error: {0}", ex. Message)); }        }

Use a regular replacement for dynamic links within a page:

         Public Static stringReplacelistandsingle (stringhtml) {HTML= Regex.Replace (HTML,@"_list\.aspx\?id= (\d*)","_list_$1.html", regexoptions.ignorecase); HTML= Regex.Replace (HTML,@"_single\.aspx\?id= (\d*)","_single_$1.html", regexoptions.ignorecase); returnHtml. Replace (". aspx",". html"); }

The dynamic page format is pagename_list.aspx?id=1 and pagename_single.aspx?id=1, and the regular replacement becomes a static format: Pagename_list_1.html and Pagename_single_1. Html.

Write the replaced page HTML to the file:

         Public Static BOOLWriteHTML (stringUrlstringhtml) {            Try            {                using(StreamWriter SW =NewStreamWriter (HttpContext.Current.Server.MapPath (URL),false, Encoding.GetEncoding ("Utf-8")) {sw.                    WriteLine (HTML); Sw.                Close (); }                return true; }            Catch(Exception ex) {Throw NewException (string. Format ("writehtml error: {0}", ex. Message)); }        }

ASP. NET generates static HTML based on existing dynamic page

Related Article

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: 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.