動態請求頁面生成靜態頁


話說百度也是這樣提高瀏覽速度的;此案例是控件開發的,MVC或一般處理程序開發的小伙伴們,修修改改一樣可以用的;

后台:

 1   protected void btnHtml_Click(object sender, EventArgs e)
 2         {
 3             if (Request.QueryString["id"] != null)
 4             {
 5                 string NewId = Request.QueryString["id"];
 6                 CJRZ.Model.Admin.LoanStrategy modeStrategy = bllNews.GetModel(int.Parse(NewId));
 7                 if (modeStrategy != null)
 8                 {
 9                     CreatHtml cHtml = new CreatHtml();
10                     string url = "http://" + HttpContext.Current.Request.Url.Host.ToString() + ":" + HttpContext.Current.Request.Url.Port;
11                     string AUrl = url + "/raiders/raiders_article.aspx?id=" + modeStrategy.NewID;
12                     //html頁面文件名
13                     string fna = Server.MapPath("\\") + "raiders\\" + modeStrategy.NewTypeID + "\\";
14                     string FileName = modeStrategy.NewID + ".html";
15                     if (cHtml.CreateList(AUrl, fna, FileName))
16                     {
17                         modeStrategy.HtmlUrl = url + "/raiders/" + modeStrategy.NewTypeID + "/" + FileName;
18                         hlkLook.NavigateUrl = modeStrategy.HtmlUrl;
19                         if (bllNews.Update(modeStrategy))
20                         {
21                             MessageBox.Show(this, "生成文件成功");
22                         }
23                         else
24                         {
25                             MessageBox.Show(this, "更新數據路徑失敗!");
26                         }
27                     }
28                     else
29                     {
30                         MessageBox.Show(this, "生成文件失敗!");
31                     }
32                 }
33                 else
34                 {
35                     MessageBox.Show(this, "獲取新聞對象失敗!");
36                 }
37 
38             }
39             else
40             {
41                 MessageBox.Show(this, "獲取ID號失敗!");
42             }
43         }

前台:

1   <asp:Button ID="btnHtml" runat="server" Text="生成靜態頁" onclick="btnHtml_Click" Visible="false" />
2                         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
3                            <asp:HyperLink ID="hlkLook" runat="server" style=" color:Blue; font-size:14px;" Target="_blank" >查看頁面</asp:HyperLink>

調用的類:

 1   public class CreatHtml
 2     {
 3         public bool CreateList(string url, string fna,string fileName)
 4         {
 5             bool ok;
 6             //准備生成
 7             string strHtml;
 8             StreamReader sr = null; //用來讀取流
 9             StreamWriter sw = null; //用來寫文件
10             Encoding code = Encoding.GetEncoding("utf-8"); //定義編碼
11 
12             //構造web請求,發送請求,獲取響應
13             WebRequest HttpWebRequest = null;
14             WebResponse HttpWebResponse = null;
15             HttpWebRequest = WebRequest.Create(url);
16             HttpWebResponse = HttpWebRequest.GetResponse();
17 
18             //獲得流
19             sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
20             strHtml = sr.ReadToEnd();
21 
22             //寫入文件
23             try
24             {
25 
26                 if (!Directory.Exists(fna)) 
27                 {
28                     Directory.CreateDirectory(fna); 
29                 }
30                 sw = new StreamWriter(fna+"\\"+fileName, false, code);
31                 sw.WriteLine(strHtml);
32                 sw.Flush();
33                 ok = true;
34             }
35             catch (Exception ex)
36             {
37                 ok = false;
38             }
39             finally
40             {
41                 if (sw != null)
42                 {
43                     sw.Close();
44                 }
45             }
46             return ok;
47         }
48     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM