在网上看了许多能生成静态页的新闻系统,但基于asp.net的系统极少,闲下时间来自己写了一个,发出来,大家一起研究,代码没做什么优化,只是实现了功能

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace makehtmlfile
{
 /// <summary>
 /// makeallfiles 的摘要说明。
 /// </summary>
 public class makeallfiles : System.Web.UI.Page
 {
  public string strcon;
  public OleDbConnection conn;
  public string strSQL;

private void Page_Load(object sender, System.EventArgs e)
  {
   
   InitialPages();// 在此处放置用户代码以初始化页面
  }

public void InitialPages()
  {
   strcon    = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";//连接字符窜// 在此处放置用户代码以初始化页面
            strSQL = "select id,class1id,class2id from news order by id desc";
   MakeAreaForShow();
   ReadNewsForWriteFileUserDataReader();      //同过DataReader来读取数据,
   //ReadNewsForWriteFileUserDataSet();      //将数据直接挂入DataSet中来读取,
  }
  /// <summary>
  /// 用来产生循环显示页面的区域,装载生成HTML页的ASPX页面的区域
  /// </summary>
  public void MakeAreaForShow()
  {
   Response.Write("<span id=showImport></span>");
   Response.Write("<IE:Download ID='oDownload' STYLE='behavior:url(#default#download)'/>");
  }

/// <summary>
  /// 通过DATAREADER来读取数据
  /// </summary>
  public void ReadNewsForWriteFileUserDataReader()
  {
   int              num        =   0   ;
   string           newsid     =   null;
   string           class1id   =   null;
   string           class2id   =   null;
   OleDbDataReader  dr         =   null;
   OleDbConnection  conn       =   new OleDbConnection(strcon);
   conn.Open();
   OleDbCommand     mycommand  =   new OleDbCommand(strSQL,conn);
   dr                          =   mycommand.ExecuteReader();
   while(dr.Read())
   {
    newsid                  =   dr["id"].ToString();
    class1id                =   dr["class1id"].ToString();
    class2id                =   dr["class2id"].ToString();
                WriteJScript(newsid,class1id,class2id);
    num++;
   }
   dr.Close();
   conn.Close();
   Response.Write(num.ToString());

}

/// <summary>
  /// 通过DATASET来读取数据
  /// </summary>
  public void ReadNewsForWriteFileUserDataSet()
  {
   DataSet          ds         =   new DataSet();
   int              num        =   0   ;
   string           newsid     =   null;
   string           class1id   =   null;
   string           class2id   =   null;

OleDbConnection  conn       =   new OleDbConnection(strcon);
   conn.Open();
   OleDbDataAdapter da         =   new OleDbDataAdapter(strSQL,conn);
   da.Fill(ds,"news");
   conn.Close();
   num                         =   ds.Tables["news"].Rows.Count;
   foreach(DataRow dr in ds.Tables["news"].Rows)
   {
    newsid     =    dr["id"].ToString();
    class1id   =    dr["class1id"].ToString();
    class2id   =    dr["class2id"].ToString();
    WriteJScript(newsid,class1id,class2id);
   }
   ds = null;
   Response.Write(num.ToString());
   
  }

public void WriteJScript(string newsid,string class1id,string class2id)
  {
   Response.Write("<script>");
   Response.Write("function onDownloadDone(downDate)");
   Response.Write("{");
   Response.Write("showImport.innerHTML=downDate");
   Response.Write("}");
   Response.Write("oDownload.startDownload('makefile2.aspx?id=");
   Response.Write(newsid);
   Response.Write("&class1id=");
   Response.Write(class1id);
   Response.Write("&class2id=");
   Response.Write(class2id);
   Response.Write("',onDownloadDone)");
   Response.Write("</script>");
  }

#region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

}
  #endregion
 }
}

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace makehtmlfile
{
 /// <summary>
 /// makefile2 的摘要说明。
 /// </summary>
 public class makefile2 : System.Web.UI.Page
 {
  public string strcon;
  public OleDbConnection conn;
  
  public string class1id;
  public string class2id;
  //***********************************
  public string previd;
  public string prevtitle;
  public string nextid;
  public string nexttitle;
  //***********************************
  public string newstitle;
  public string newscontent;
  public string newsdate;
  public string newsip;
  public string newsid;
  //************************************

private void Page_Load(object sender, System.EventArgs e)
  {
   strcon    = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";//连接字符窜// 在此处放置用户代码以初始化页面
   if(Request.Params["id"]!=null&&Request.Params["class1id"]!=null&&Request.Params["class2id"]!=null)
   {
    InitialPages();

}// 在此处放置用户代码以初始化页面
  }

public void InitialPages()
  {
   strcon    = "provider=Microsoft.jet.OLEDB.4.0;data Source="+Server.MapPath(ConfigurationSettings.AppSettings["MDBpath2"])+";";
   
   if(Request.Params["id"]!=null)
   {
    newsid  = Request.Params["id"].ToString();
   }
   if(Request.Params["class1id"]!=null)
   {
    class1id  = Request.Params["class1id"].ToString();
   }
   if(Request.Params["class2id"]!=null)
   {
    class2id  = Request.Params["class2id"].ToString();
   }
   ReadDataBase(newsid,class2id);
   MakeHtmlFile(newsid,class1id,class2id);
  }

/// <summary>
  /// 读写同一分类中,上一篇,和下一篇文章
  /// </summary>
  /// <param name="inputid"> 该文章ID</param>
  /// <param name="class2id">该文章所属分类ID</param>
  public void ReadPrevAndNext(string inputid,string class2id)
  {
   int               id                 =    int.Parse(inputid);
   string            strPrevSQL         =    "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id<"+id+" order by id desc";
   string            strNextSQL         =    "select top 1 id,newstitle,newsdate from news where class2id='"+ class2id +"' and id>"+id+" order by id asc";
   OleDbDataReader   datar              =    null;
   OleDbConnection con       = new OleDbConnection(strcon);
   con.Open();
   OleDbCommand newcommand   = new OleDbCommand(strPrevSQL,con);
   datar                     = newcommand.ExecuteReader();
   while(datar.Read())
   {
    previd    =  datar["id"].ToString();
    prevtitle =  datar["newstitle"].ToString();
   }
   datar.Close();
   newcommand.CommandText = strNextSQL ;
   datar                  = newcommand.ExecuteReader();
   while(datar.Read())
   {
    nextid     =  datar["id"].ToString();
    nexttitle  =  datar["newstitle"].ToString();
   }
   con.Close();

}

/// <summary>
  /// 将文章信息从库中读出,并将准备生成的HTML文件路径写入库中
  /// </summary>
  /// <param name="inputid"></param>
  /// <param name="class2id"></param>
  public void ReadDataBase(string inputid,string class2id)
  {
   string filename_w = MakeFileName(class1id,class2id,newsid)+".htm";
   
   ReadPrevAndNext(inputid,class2id);       //读取下一篇和上一篇的信息。

OleDbConnection mycon  = new OleDbConnection(strcon);          //打开数据库连接
   mycon.Open();

int              id    = int.Parse(inputid);
   string       strSQL    = "select * from news where id="+id;
   OleDbDataReader  dr    = null;
   OleDbCommand mycommand = new OleDbCommand(strSQL,mycon);
   dr                     = mycommand.ExecuteReader();
   while(dr.Read())
   {
    newstitle   = dr["newstitle"].ToString();
    newscontent = dr["newscontent"].ToString();
    newsdate    = dr["newsdate"].ToString();
    newsip      = dr["newsip"].ToString();
   }
   dr.Close();
   mycommand.CommandText = "update news set url='"+ filename_w +"' where id="+int.Parse(inputid);   //将生成的文件路径写入库中,以遍在生成分类页中方便使用
   mycommand.ExecuteNonQuery();
   mycon.Close();
   
  }

/// <summary>
  /// 生成目标目录和文件,主要用来生成不同分类的目录
  /// </summary>
  /// <param name="inputstr"></param>
  /// <returns></returns>
  public string MakeCatalogName(string class1,string class2)            //生成目标目录文件
  {
   string namestr   = "Article";
   string rootstr   = Server.MapPath(".").ToString();
   string class1str  = rootstr + "//" + namestr + "_" + class1 + "//";
   string class2str  = rootstr + "//" + namestr + "_" + class1 + "//" + namestr + "_" + class2 + "//";
   if(!Directory.Exists(class1str))
   {
    Directory.CreateDirectory(class1str);
   }
   if(!Directory.Exists(class2str))
   {
    Directory.CreateDirectory(class2str);
   }
              //创建目标文件夹
   return class2str;
  }

/// <summary>
  /// 根据文章分类和ID生成文件名
  /// </summary>
  /// <param name="class1id"></param>
  /// <param name="class2id"></param>
  /// <param name="nid"></param>
  /// <returns>返回文件名</returns>
  public string MakeFileName(string class1,string class2,string id)    //生成文件名,能够生成上下篇
  {
   string myclass2id    = class2;
   string myclass1id    = class1;
   string s             = DateTime.Now.Year.ToString()
    +DateTime.Now.Month.ToString()
    +DateTime.Now.Day.ToString()
    +"_"
    +myclass1id 
    +"_"
    +myclass2id                         //父类ID
    +"_"
    +id;                               //新闻ID
   return s;
  }

/// <summary>
  /// 生成HTML文件
  /// </summary>
  /// <param name="nid">文章ID号</param>
  public void MakeHtmlFile(string nid,string cla1id,string cla2id)                      //  MakeHtmlFile(string nid,string cla1id,string cla2id,string filetemp)  用于区分不同的摸班
  {
   string file_path            =   Server.MapPath ("template/news_mb.htm");
   string desfilename          =   MakeFileName(cla1id,cla2id,nid)+".htm";
   string desfile              =   MakeCatalogName(cla1id,cla2id)+MakeFileName(cla1id,cla2id,nid)+".htm";
   string prevurl              =   MakeFileName(cla1id,cla2id,previd)+".htm";                 //根据分类和ID生成上下篇的文件连接名
   string nexturl              =   MakeFileName(cla1id,cla2id,nextid)+".htm";                 //下篇
   System.Text.Encoding code   =   System.Text.Encoding.GetEncoding("gb2312");
  
   StreamReader srd            =   null;        //读
   StreamWriter swr            =   null;        //写
  
   string strFile              =   null ;       //字符串
  
   try
   {
    srd                     =   new StreamReader(file_path, code);
    strFile                 =   srd.ReadToEnd(); // 读取文件
   }
   catch(Exception exp)
   {
    HttpContext.Current.Response.Write(exp.Message);
    HttpContext.Current.Response.End();
    srd.Close();
   }
  
   strFile = strFile.Replace("$title$",newstitle);
   strFile = strFile.Replace("$content$",newscontent);
   strFile = strFile.Replace("$date$",newsdate);
   strFile = strFile.Replace("$ip$",newsip);
   strFile = strFile.Replace("$prev$",prevtitle);
   strFile = strFile.Replace("$next$",nexttitle);
   strFile = strFile.Replace("$prevurl$",prevurl);
   strFile = strFile.Replace("$nexturl$",nexturl);
 
   try
   {
    swr = new StreamWriter(desfile,false,code);
    swr.Write(strFile);
    swr.Flush();
   }
   catch(Exception ex)
   {
    HttpContext.Current.Response.Write(ex.Message);
    HttpContext.Current.Response.End();
   }
   finally
   {
    swr.Close();
   }
   if(srd!=null)
   {
    srd.Close();
   }
   Response.Write(desfilename);

}

public void Open()
  {
   if(conn==null)
   {
    conn = new OleDbConnection(strcon);
    conn.Open();
   }
  }
  public void Close()
  {
   if(conn!=null)
   {
    conn.Close();
   }
  }

#region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
 }
}

生成静态文件的新闻系统核心代码相关推荐

  1. 制作生成静态页面的新闻系统

    利用PHP生成静态HTML页面的好处很多: 1.静态页面不需要Web服务器解释执行,用户打开网页的速度会快些: 2.打开静态页面时,Web服务器不需要访问数据库,减轻了对数据库访问的压力: 3.静态H ...

  2. phpcms v9电脑pc站+手机wap移动端双模板共用数据库数据同步可同步生成静态文件

    phpcms v9电脑pc站+手机wap移动端双模板共用数据库数据同步可同步生成静态文件,并且电脑站和手机站网址页面一一对应,非插件,程序二次开发版. 详情如下: 1.phpcms v9电脑版+手机版 ...

  3. ASP:关于生成HTML文件的新闻系统

    一般的传递ID值的新闻系统见得比较多,制作起来也不是很复杂. 但是我们在新浪或是其它的门户类网站看到到的新闻不是用ID传递的,而是一个HTML或是Shtml文件,难道手工加上去的吗?当然不是了,其实这 ...

  4. java 生成静态html的一段代码

    在某些应用中,将本该由动态页面每次获取客户端请求时去调用数据的过程转换为在添加数据时即生成为静态页面,对服务器的压力,数据库检索的压力,以及搜索引擎收录,包括防止SQL注入都是由极大的好处的.常见的做 ...

  5. 将PHP文件生成静态文件源码

    开心返利网   5i321网址导航 将PHP文件转换成静态文件,这样就不用频繁读取数据库了,打开速度也会快很多 <?php     function mhtml($url,$fileName){ ...

  6. C#生成CHM文件(应用篇)之代码库编辑器(1)

    下面的这个系列是以一个小软件的开发过程为基础,和大家分享下我在开发这个软件时遇到的一些问题和总结. 先更新下软件:代码库编辑器Beta版下载 ,上一版的程序见<C#生成CHM文件(应用篇)> ...

  7. tp5 File文件写入(生成静态文件)

    利用\think\template\driver\File.php文件整合buildHtml 1.在controller.php创建方法如下 /** * 创建静态页面 * @access protec ...

  8. C#生成CHM文件(应用篇)之代码库编辑器(5)【总结、程序、源代码】

    经历了快一个月的开发(因为都是在闲暇时间做的,实际实际可能不到一周),AlexisEditor总算完成了. 这边说明一下为什么有些网友不能新增文章的问题. 原因是路径中有中文字符,我一直用的是英文系统 ...

  9. 12月最新仿知音漫画网站源码+手机端,小说漫画生成静态文件,超强负载量安全可靠

    解压密码bbs.youyacao.com csdn下载地址: https://download.csdn.net/download/dujiangdu123/13685884 1.首先上传程序至您的主 ...

最新文章

  1. 人工智能顶会WSDM2021 Best Paper Award 发布!
  2. 企业安全建设之搭建开源SIEM平台(上)
  3. 在java中的ascii_在Java中绘制ASCII艺术
  4. 三菱plcfx5u指令手册_从西门子200的PLC程序来看三菱FX5U的PLC程序
  5. request获取url的参数编码问题
  6. 如何在weblogic启动时让其加载指定的jar库文件
  7. MySQL自定义查询字段排序
  8. hdu 6395Sequence【矩阵快速幂】【分块】
  9. 松下机器人找原点步骤_桁架机器人在汽车座椅安装生产线中的应用
  10. java银联接口代码_银联接口测试——详细(JAVA)(示例代码)
  11. 异常总结2013-04
  12. mysql查询每个表占用空间,【MySQL】查询所有数据库占用磁盘空间大小和单个库中所有表的大小...
  13. (5)剑指Offer之栈变队列和栈的压入、弹出序列
  14. bdfg的matlab仿真模型,无刷双馈风力发电机变速恒频控制研究
  15. 三十一、利用微信搜索抓取公众号文章
  16. 基于JavaSwing的餐厅点餐系统
  17. python实验总结_python实训总结和体会_python实训心得体会 - CSDN
  18. 2016年的年终总结吧
  19. springboot+vue项目部署到外网服务器的完整步骤(前后端分离 分别部署)
  20. 项目实战-----产品经理要做什么?

热门文章

  1. mysql www.school.com_MySQL 基础学习
  2. PCL:英文参考链接
  3. PCL调错(2):VTK报错
  4. JAVA导出exls时报oom_如何实现导出百万条数据到EXCEL中不报OOM异常?
  5. 深入浅出的讲解傅里叶变换(完整)
  6. 最精简写法→去掉任意多个空行
  7. 【只需4步】windows server系统下快速安装绿色版apache-tomcat-8.0.35(免安装版)
  8. 2021-08-05 Ubuntu18.04安装ROS出现的一些问题
  9. 在CentOS 6.9 x86_64上安装nginx 1.12.2
  10. ValueError: invalid literal for int() with base 10