• 1
  • 2
  • 3
  • 4
  • 5
asp.net空间 首 页  »  帮助中心  »  虚拟主机  »  asp.net空间
如何.net生成静态HTML页
发布日期:2015-10-14 11:10:58

  如何.net生成静态HTML页

  之前介绍了.net生成静态HTML页好处及必要性,也有许多人来询问具体编程。这里为大家方上具体的编码,供您参考,希望能够帮到您:

  /首先,生成一个HTML页

  public static bool WriteFile(string strText,string strContent,string strAuthor)

  {

  string path = HttpContext.Current.Server.MapPath("/news/");

  Encoding code = Encoding.GetEncoding("gb2312");

  // 读取文件

  string temp = HttpContext.Current.Server.MapPath("/news/text.html");

  StreamReader sr=null;

  StreamWriter sw=null;

  string str="";

  try

  {

  sr = new StreamReader(temp, code);

  str = sr.ReadToEnd();

  }

  catch(Exception exp)

  {

  HttpContext.Current.Response.Write(exp.Message);

  HttpContext.Current.Response.End();

  sr.Close();

  }

  string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";

  // 替换内容

  str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle

  str = str.Replace("biaoti",strText);

  str = str.Replace("content",strContent);

  str = str.Replace("author",strAuthor);

  // 写入文件

  try

  {

  sw = new StreamWriter(path + htmlfilename , false, code);

  sw.Write(str);

  sw.Flush();

  }

  catch(Exception ex)

  {

  HttpContext.Current.Response.Write(ex.Message);

  HttpContext.Current.Response.End();

  }

  finally

  {

  sw.Close();

  }

  return true;

  if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))

  {

  Response.Write("添加成功");

  }

  else

  {

  Response.Write("生成HTML出错!");

  }