asp.net產生HTML靜態頁

來源:互聯網
上載者:User
using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Text.RegularExpressions;using System.IO;/// <summary>/// CreateHtml 的摘要說明/// asp.net產生HTML靜態頁/// 調用方法:/// new CreateHtml().Nei_Create("http://localhost:4032/new5mdn/default.aspx", "default.htm"); /// </summary>public class CreateHtml :System.Web.UI.Page{    public CreateHtml()    {            }    /// <summary>    /// 產生靜態頁面,產生位置是本項目下    /// </summary>    /// <param name="strURL">請求的URL</param>    /// <param name="strRelPath">建立的路徑及檔案名稱,路徑為相對路徑</param>    /// <returns></returns>    public bool Nei_Create(string strURL, string strRelPath)    {        string strFilePage;        strFilePage = HttpContext.Current.Server.MapPath(strFilePage);        StreamWriter sw = null;        //獲得aspx的靜態html        try        {            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(strURL);            System.Net.WebResponse wResp = wReq.GetRequestStream();            System.IO.Stream respStream = wResp.GetResponseStream();            System.IO.StreamReader reader = new StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));            string strTemp = reader.ReadToEnd();            Regex r1 = new Regex("<input type=\"hidden\" name=\"__EVENTTARGET\".*/>", RegexOptions.IgnoreCase);            Regex r2 = new Regex("<input type=\"hidden\" name=\"__EVENTARGUMENT\".*/>", RegexOptions.IgnoreCase);            Regex r3 = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\".*/>", RegexOptions.IgnoreCase);            Regex r4 = new Regex("<form .*id=\"form1\">", RegexOptions.IgnoreCase);            Regex r5 = new Regex("</form>");            Regex r6 = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\".*/>", RegexOptions.IgnoreCase);            strTemp = r1.Replace(strTemp, "");            strTemp = r2.Replace(strTemp, "");            strTemp = r3.Replace(strTemp, "");            strTemp = r4.Replace(strTemp, "");            strTemp = r5.Replace(strTemp, "");            strTemp = r6.Replace(strTemp, "");            sw.Write(strTemp);           }        catch(Exception ex)        {            HttpContext.Current.Response.Write(ex.Message);            HttpContext.Current.Response.End();            return false; //產生到出錯        }        finally        {            sw.Flush();            sw.Close();            sw = null;        }    }    /// <summary>       /// 產生靜態頁面,產生位置不在本項目下       /// </summary>       /// <param name="strURL">請求的URL</param>       /// <param name="strRelPath">建立的路徑及檔案名稱,路徑為絕對路徑</param>       public bool Wai_Create(string strURL, string strRelPath, string filename)    {        string strFilePage;        strFilePage = strRelPath + "\\" + filename;        StreamWriter sw = null;        //獲得aspx的靜態html           try        {            if (!Directory.Exists(strRelPath))            {                Directory.CreateDirectory(strRelPath);            }            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(strURL);            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"));            string strTemp = reader.ReadToEnd();            Regex r1 = new Regex("<input type=\"hidden\" name=\"__EVENTTARGET\".*/>", RegexOptions.IgnoreCase);            Regex r2 = new Regex("<input type=\"hidden\" name=\"__EVENTARGUMENT\".*/>", RegexOptions.IgnoreCase);            Regex r3 = new Regex("<input type=\"hidden\" name=\"__VIEWSTATE\".*/>", RegexOptions.IgnoreCase);            Regex r4 = new Regex("<form .*id=\"form1\">", RegexOptions.IgnoreCase);            Regex r5 = new Regex("</form>");            Regex r6 = new Regex("<input type=\"hidden\" name=\"__EVENTVALIDATION\".*/>", RegexOptions.IgnoreCase);            strTemp = r1.Replace(strTemp, "");            strTemp = r2.Replace(strTemp, "");            strTemp = r3.Replace(strTemp, "");            strTemp = r4.Replace(strTemp, "");            strTemp = r5.Replace(strTemp, "");            strTemp = r6.Replace(strTemp, "");            sw.Write(strTemp);        }        catch (Exception ex)        {            HttpContext.Current.Response.Write(ex.Message);            HttpContext.Current.Response.End();            return false;//產生到出錯           }        finally        {            sw.Flush();            sw.Close();            sw = null;        }        return true;    }       public void FilePicDelete(string path)    {        System.IO.FileInfo file = new System.IO.FileInfo();        if (file.Exists)            file.Delete();    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.