二種產生靜態網頁的方法(asp.net)

來源:互聯網
上載者:User
      ASP.NET產生靜態頁,主要有二種方式.分別是通過模板和根據url產生

  通過輸入內容替換模板或者url地址兩種方式進行靜態檔案的產生

  templete.htm為模板檔案,htm為產生後的靜態檔案儲存位置

  下面是.cs檔案

 2  public partial class _Default : System.Web.UI.Page
 3  {
 4    protected void Page_Load(object sender, EventArgs e)
 5    {
 6     
 7    }
 8
 9    //根據模板產生,保持在html檔案夾中(部分源碼搜集於網路)
10    protected void Button1_Click(object sender, EventArgs e)
11    {
12      //源碼是替換掉模板中的特徵字元
13
14      string mbPath =Server.MapPath("template.htm");
15      Encoding code = Encoding.GetEncoding("gb2312");
16      StreamReader sr = null;
17      StreamWriter sw = null;
18      string str = null;
19
20      //讀取
21      try
22      {
23        sr = new StreamReader(mbPath, code);
24        str = sr.ReadToEnd();
25
26      }
27      catch (Exception ex)
28      {
29        throw ex;
30      }
31      finally
32      {
33        sr.Close();
34      }
35
36      //根據時間自動重新命名,副檔名也可以自行修改
37      string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
38      str = str.Replace("$title$", txtTitle.Text);//替換Title
39      str = str.Replace("$content$", txtContent.Text);//替換content
40
41      //產生靜態檔案
42      try
43      {
44        sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
45        sw.Write(str);
46        sw.Flush();
47
48      }
49      catch (Exception ex)
50      {
51        throw ex;
52      }
53      finally
54      {
55        sw.Close();
56        Response.Write("恭喜<a href=htm/"+fileName+" target=_blank>"+fileName+"</a>已經產生,儲存在htm檔案夾下!");
57      }
58
59
60    }
61
62
63    //網頁製作:根據Url地址產生靜態頁保持
64    protected void Button2_Click(object sender, EventArgs e)
65    {
66      Encoding code = Encoding.GetEncoding("utf-8");
67      StreamReader sr = null;
68      StreamWriter sw = null;
69      string str = null;
70
71      //讀取遠程路徑
72      WebRequest temp = WebRequest.Create(txtUrl.Text.Trim());
73      WebResponse myTemp = temp.GetResponse();
74      sr = new StreamReader(myTemp.GetResponseStream(), code);
75      //讀取
76      try
77      {
78        sr = new StreamReader(myTemp.GetResponseStream(), code);
79        str = sr.ReadToEnd();
80
81      }
82      catch (Exception ex)
83      {
84        throw ex;
85      }
86      finally
87      {
88        sr.Close();
89      }
90      string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".htm";
91
92      //寫入
93      try
94      {
95        sw = new StreamWriter(Server.MapPath("htm/") + fileName, false, code);
96        sw.Write(str);
97        sw.Flush();
98
99      }
100      catch (Exception ex)
101      {
102        throw ex;
103      }
104      finally
105      {
106        sw.Close();
107        Response.Write("恭喜<a href=htm/" + fileName + " target=_blank>" + fileName + "</a>已經產生,儲存在htm檔案夾下!");
108      }
109
110    }
111  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.