麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

Asp.net生成靜態網頁的實現代碼

2019-11-14 13:30:18
字體:
來源:轉載
供稿:網友

現在做程序都要將動態的頁面轉換成靜態頁面,今天教大家在asp.net 中實現靜態頁面的生成方法。

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; 
  } 
  } 
  調用公共類: 
  ---------------------------------------------------------------------------- 
  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文件放在里面

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

  增加一個模板文件

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head> 
    <title>無標題頁</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"> 
                發布人:@UserName &nbsp; &nbsp; 發布時間:@PubTime &nbsp; &nbsp;&nbsp; 新聞類別:@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>

轉載自:http://www.aspnetjia.com


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚州综合网 | 久久国产精品久久久久久久久久 | 亚洲白嫩在线观看 | 亚洲无线看 | 国产超碰人人做人人爱 | 国产做爰全免费的视频黑人 | 久久国产免费视频 | 国产成人高潮免费观看精品 | chengrenzaixian| 九九视频久久 | 欧美 日韩 亚洲 中文 | 久久精品视频亚洲 | 91短视频在线免费观看 | 九九热精品免费 | 成人黄色网战 | 成人毛片100免费观看 | 日韩精品羞羞答答 | 91在线精品亚洲一区二区 | 国产精品自在线拍 | 99精品国产视频 | 福利在线免费视频 | 女人裸体让男人桶全过程 | 全黄毛片| 亚洲看片网 | 久久久久一区二区三区四区五区 | 国产羞羞视频在线免费观看 | 水多视频在线观看 | 久久影院在线观看 | 免费国产自久久久久三四区久久 | 日日狠狠久久偷偷四色综合免费 | 国产精品视频海角社区88 | 欧美日本在线视频 | 欧美精品一区自拍a毛片在线视频 | 国产精品99久久久久久久vr | 热久久成人 | 欧美在线观看视频一区 | 大学生a级毛片免费视频 | 午夜激情视频网站 | 天天鲁在线视频免费观看 | 青草久久网 | 久久久久免费精品国产小说色大师 |