ASP. NET dynamically generates static pages based on URLs

Source: Internet
Author: User
Two methods

Method 1:

/** // <Summary>
/// Static page generation method
/// </Summary>
/// <Param name = "strPageUrl"> Generate source </param>
/// <Param name = "strFileName"> generated to </param>
Private bool MakePage (string strPageUrl, string strFileName)
{
String strDir, strFilePage;
StrDir = @ "/htm/MakePage/"; // The updated folder.
StrFilePage = Server. MapPath (strDir + strFileName );

StreamWriter sw = null;
// Obtain the static html of aspx
Try
{
If (! Directory. Exists (Server. MapPath (strDir )))
{
Directory. CreateDirectory (Server. MapPath (strDir ));
}
If (File. Exists (strFilePage ))
{
File. Delete (strFilePage );
}
Sw = new StreamWriter (strFilePage, false, System. Text. Encoding. GetEncoding ("GB2312 "));
Server. Execute (strPageUrl, sw );
}
Catch (Exception ex)
{
Msg. ShowMsg ("'" + strFileName + "' generation error:" + ex. Message );
Return false; // An error occurred while generating the code.
}
Finally
{
Sw. Flush ();
Sw. Close ();
Sw = null;
}

Return true;
}

Method 2:/** // <summary>
/// Static page generation method
/// </Summary>
/// <Param name = "strPageUrl"> Generate source </param>
/// <Param name = "strFileName"> generated to </param>
Private bool MakePage (string strPageUrl, string strFileName)
{
String strDir, strFilePage;
StrDir = @ "/htm/MakePage/"; // The updated folder.
StrFilePage = Server. MapPath (strDir + strFileName );

StreamWriter sw = null;
// Obtain the static html of aspx
Try
{
If (! Directory. Exists (Server. MapPath (strDir )))
{
Directory. CreateDirectory (Server. MapPath (strDir ));
}
If (File. Exists (strFilePage ))
{
File. Delete (strFilePage );
}
Sw = new StreamWriter (strFilePage, false, System. Text. Encoding. GetEncoding ("GB2312 "));
System. Net. WebRequest wReq = System. Net. WebRequest. Create (strPageUrl );
System. Net. WebResponse wResp = wReq. GetResponse ();
System. IO. Stream respStream = wResp. GetResponseStream ();
System. IO. StreamReader reader = new System. IO. StreamReader (respStream, System. Text. Encoding. GetEncoding ("gb2312 "));
Sw. Write (reader. ReadToEnd ());
}
Catch (Exception ex)
{
Msg. ShowMsg ("'" + strFileName + "' generation error:" + ex. Message );
Return false; // An error occurred while generating the code.
}
Finally
{
Sw. Flush ();
Sw. Close ();
Sw = null;
}

Return true;
}

Summary: method 1 can only use virtual paths (e.g., cannot use http://www.qq.com), and method 2 is opposite.

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.