国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Asp.net生成靜態(tài)網(wǎng)頁的實(shí)現(xiàn)代碼

2019-11-14 13:30:18
字體:
供稿:網(wǎng)友

現(xiàn)在做程序都要將動(dòng)態(tài)的頁面轉(zhuǎn)換成靜態(tài)頁面,今天教大家在asp.net 中實(shí)現(xiàn)靜態(tài)頁面的生成方法。

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.IO; 
  using System.Text; 
  ///  
  /// WriteFile 的摘要說明 
  ///  
  public class WriteFile 
  { 
  public WriteFile() 
  { 
  } 
  public static bool createHtml(string[] strnewsHtml,string[] strolDHTML,string strModeFilePath,string strPath) 
  { 
  bool flag = false; 
  StreamReader sr = null; 
  StreamWriter sw = null; 
  string filepath = HttpContext.Current.Server.MapPath(strModeFilePath); 
  Encoding code = Encoding.GetEncoding("gb2312"); 
  string s = string.Empty; 
  try 
  { 
  sr = new StreamReader(filepath,code); 
  s = sr.ReadToEnd(); 
  } 
  catch (Exception ex) 
  { 
  throw ex; 
  } 
  finally 
  { 
  sr.Close(); 
  } 
  try 
  { 
  for (int i = 0; i < strnewsHtml.Length; i++) 
  { 
  s = s.Replace(stroldHtml[i], strnewsHtml[i]); 
  } 
  sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code); 
  sw.Write(s); 
  flag = true; 
  } 
  catch (Exception ex) 
  { 
  flag = false; 
  throw ex; 
  } 
  finally 
  { 
  sw.Flush(); 
  sw.Close(); 
  } 
  return flag; 
  } 
  public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml) 
  { 
  bool Flage = false; 
  StreamReader ReaderFile = null; 
  StreamWriter WrirteFile = null; 
  string FilePath = HttpContext.Current.Server.MapPath(strHtml); 
  Encoding Code = Encoding.GetEncoding("gb2312"); 
  string strFile = string.Empty; 
  try 
  { 
  ReaderFile = new StreamReader(FilePath, Code); 
  strFile = ReaderFile.ReadToEnd(); 
  } 
  catch (Exception ex) 
  { 
  throw ex; 
  } 
  finally 
  { 
  ReaderFile.Close(); 
  } 
  try 
  { 
  int intLengTh = strNewsHtml.Length; 
  for (int i = 0; i < intLengTh; i++) 
  { 
  int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length; 
  int intEnd = strFile.IndexOf(strEndHtml[i]); 
  string strOldHtml = strFile.Substring(intStart, intEnd - intStart); 
  strFile = strFile.Replace(strOldHtml, strNewsHtml[i]); 
  } 
  WrirteFile = new StreamWriter(FilePath, false, Code); 
  WrirteFile.Write(strFile); 
  Flage = true; 
  } 
  catch (Exception ex) 
  { 
  throw ex; 
  } 
  finally 
  { 
  WrirteFile.Flush(); 
  WrirteFile.Close(); 
  } 
  return Flage; 
  } 
  } 
  調(diào)用公共類: 
  ---------------------------------------------------------------------------- 
  PRotected void Button2_Click(object sender, EventArgs e) 
  { 
  string NewsTitle = this.TextBox1.Text; 
  string NewsKindName = this.DropDownList1.SelectedItem.Text; 
  string NewsBody = this.WebEditor1.Text; 
  DateTime PubTime = DateTime.Now; 
  string UserName = session["UserName"].ToString(); 
  Response.Write(NewsKindName); 
  string[] strNewsHtml = new string[] { NewsTitle, NewsKindName, NewsBody, PubTime.ToString(), UserName }; 
  string[] strOldHtml = new string[] { "@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName" }; 
  string strFileName = DateTime.Now.ToString("ddhhmmss") + ".html"; 
  string strFilePath = string.Format("NewsHtml/{0}", strFileName); 
  try 
  { 
  if (WriteFile.createHtml(strNewsHtml, strOldHtml, "mode.htm", strFilePath)) 
  { 
  this.Label1.Text = "生成成功!"; 
  } 
  else 
  { 
  this.Label1.Text = "生成失敗!"; 
  } 
  } 
  catch 
  { 
  this.Label1.Text = "生成失敗!"; 
  } 
  } 
  protected void Button3_Click(object sender, EventArgs e) 
  { 
  string[] strNewsHtml=new string[]{"到此一游!"}; 
  string[] strStartHtml=new string[]{""}; 
  string[] strEndHtml=new string[]{""}; 
  if (WriteFile.UpdateHtmlPage(strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/02011139.html")) 
  { 
  this.Label1.Text="生成首頁成功!"; 
  } 
  else 
  { 
  this.Label1.Text="生成首頁失敗!"; 
  } 
  }

  新建文件夾NewsHtml,生成html文件放在里面

  -----------------------------------------------------------

  增加一個(gè)模板文件

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
    <title>無標(biāo)題頁</title> 
</head> 
<body> 
    <table border="1" cellpadding="0" cellspacing="0" style="width: 522px; height: 338px"> 
        <tr> 
            <td align="center" colspan="2"> 
                @Title</td> 
</tr> 
        <tr> 
            <td align="center" colspan="2"> 
                發(fā)布人:@UserName &nbsp; &nbsp; 發(fā)布時(shí)間:@PubTime &nbsp; &nbsp;&nbsp; 新聞?lì)悇e:@NewsKInd</td> 
        </tr> 
        <tr> 
            <td colspan="2"> 
                @NewsBody</td></tr><tr> 
  <td style="WIDTH: 100px">  
</td><td style="WIDTH: 100px" > 
</td></tr></table></body></html>

轉(zhuǎn)載自:http://www.aspnetjia.com


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 八宿县| 麦盖提县| 北安市| 静海县| 项城市| 法库县| 尖扎县| 章丘市| 苍梧县| 靖宇县| 万荣县| 务川| 曲沃县| 双鸭山市| 长丰县| 乐平市| 二手房| 庐江县| 廊坊市| 开阳县| 花垣县| 文昌市| 尤溪县| 江孜县| 衢州市| 三明市| 宜兴市| 焦作市| 壶关县| 太谷县| 兴安盟| 西藏| 长垣县| 子长县| 龙州县| 绵竹市| 图们市| 浮梁县| 岐山县| 疏附县| 鸡西市|