ASP. NET MVC page static function implementation of two: using recursive algorithm to achieve

Source: Internet
Author: User

The previous article mentions the use of IHttpModule to achieve when a user visits a Web site, by redefining the Response.filter to implement the HTML code that is returned to the client, so that the next time the user accesses a static page directly.

ASP. NET MVC page static feature implementation one: Use IHttpModule, abandon Resultfilter

Later thought can obtain the HTML code through the WebRequest, then uses the recursive algorithm to implement. The basic implementation ideas are as follows:

Get the HTML code returned by WebRequest to the hyperlink address, and save it; match all hyperlinks in HTML code href= "" Inside address information through regular expressions, and then recursively WebRequest get HTML code by passing the hyperlink address

The implementation code is as follows:

 Public classhtmlpagehelper{PrivateArrayList htmlcreatedlist =NewArrayList (); /// <summary>    ///recursive implementation of page static function/// </summary>    /// <param name= "urlstring" >the page link address to access</param>     Public voidSavehtmlcode (stringurlstring) {            if(Htmlcreatedlist.contains (urlstring)) {return; }            stringHtmlcode =Gethtmlcodefromurl (urlstring); stringHtmlpath =Gethtmlpathfromurl (urlstring); stringDirechtmlpath =Path.getdirectoryname (Htmlpath); if(!directory.exists (Direchtmlpath))            {directory.createdirectory (Direchtmlpath);            } file.writealltext (Htmlpath, Htmlcode);            Htmlcreatedlist.add (urlstring); ArrayList urllist=Geturllinkfromhtmlcode (Htmlcode); stringUrltemp =string.            Empty; foreach(stringUrlinchurllist) {Urltemp=URL; Urltemp= Regex.Replace (Urltemp,"href\\s*=\\s*",""); Urltemp= Urltemp.replace ("\"",""); Urltemp= Urltemp.replace ("\\","/"); Urltemp= Webconfiginfo.urlprefix +urltemp;            Savehtmlcode (urltemp); }        }    /// <summary>    ///HTML code linked through the HttpWebRequest page/// </summary>    /// <param name= "urlstring" >page link Address</param>    /// <returns>HTML code for page links</returns>    Private stringGethtmlcodefromurl (stringurlstring) {HttpWebRequest Hwrequest=(HttpWebRequest) webrequest.create (urlstring); Hwrequest.useragent="user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;. NET CLR 1.0.3705"; Hwrequest.accept="*/*"; Hwrequest.keepalive=true; HWREQUEST.HEADERS.ADD ("Accept-language","zh-cn,en-us;q=0.5"); HttpWebResponse Hwresponse=(HttpWebResponse) hwrequest.getresponse (); Stream Streamresponse=Hwresponse.getresponsestream (); StreamReader Readerofstream=NewStreamReader (Streamresponse, System.Text.Encoding.GetEncoding ("Utf-8")); stringstrHTML =Readerofstream.readtoend ();            Readerofstream.close ();            Streamresponse.close ();            Hwresponse.close (); returnstrhtml; }    /// <summary>    ///analyze the page link address to generate a static page save physical path/// </summary>    /// <param name= "urlstring" >page link Address</param>    /// <returns>physical path for static page saving</returns>    Private stringGethtmlpathfromurl (stringurlstring) {URI Uri=NewUri (urlstring); stringFilePath = HttpContext.Current.Request.PhysicalApplicationPath +"Html"+ URI. Absolutepath +"\\"; string[] Querys = URI. Query.split (New Char[] {'?','&','='}, Stringsplitoptions.removeemptyentries); foreach(stringQueryinchQuerys) {FilePath+=query; } FilePath+ = Querys. Length.equals (0) ?"index.html":". html"; FilePath= Filepath.replace ("/","\\"); FilePath= Filepath.replace ("\\\\","\\"); returnFilePath; }    /// <summary>    ///Regular Expressions match hyperlinks in HTML code/// </summary>    /// <param name= "Htmlcode" >to find the HTML code for the hyperlink</param>    /// <returns></returns>    PrivateArrayList Geturllinkfromhtmlcode (stringHtmlcode) {ArrayList alist=NewArrayList (); stringStrregex ="href\\s*=\\s* (?: [\ "] (? <1>[^\" '. #:]*) [\ "])"; Regex R=NewRegex (Strregex, regexoptions.ignorecase); MatchCollection m=r.matches (Htmlcode);  for(inti =0; I <= M.count-1; i++)            {                stringStrnew = M[i]. ToString (). Replace ("amp;",""); if(!alist.contains (strnew))                {Alist.add (strnew); }            }            returnalist; }}

ASP. NET MVC page static function implementation of two: using recursive algorithm to achieve

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.