前天在园子里把
在ASP.net中网站访问量统计方法 一文贴了上去,觉得对刚学做网站的朋友还有点用处,也有好多朋友留言鼓励,也有朋友指出了存在的问题,对我很受启发。今天是休息日,我就把在“
独上高楼”网站上原创一这篇文章又贴了上来,敬请朋友指教,本文是参考了
大型网站新闻系统如何生成静态网页、
ASP.NET生成静态网页的方法 等文章后形成的思路,同时在本人网站上实现了批量转换。以下
所发布的代码均为我个人网站实际运行的代码,读者可以作适当的修改后相信能在你的系统中运行。本网站新闻分类为三级,即大类、小类、三级类,主页面为VS.net动态页面,要实现一次性自动批量生成HTML页面和索引页面,需要解决如下问题:
1.在添加和修改文章时,自动生成HTML页面;
2.根据新闻分类自动判断和创建存储HTML页面的目录;
3.按三级类、小类、大类自动批量生成HTML页面,或者一次性生成所有的HTML页面和HTML索引页面;
一、根据新闻分类自动判断和创建存储HTML页面的目录
在添加和修改新闻时,需要对新闻所属类别进行选择或者确认,本网站就是根据新闻三级分类来创建存储HTML页面目录的,所有索引目录存储在“HTML”目录下,所有HTML文件存储在对应的第三级目录下,如123.html存储在:../ntml/1/11/111/123.html。HTML文件名均采用文章的ID编号命名。
自动判断和创建存储HTML页面的目录的代码如下,解释包含在代码中,这里不重复了:
//这里自动生成Html文件
//首先判断是否要创建按照ID三级文件夹
string ClassPathA = this.txtClass1.Text.Trim(); // ClassPathA为大类码
string ClassPathB = this.txtClass2.Text.Trim(); // ClassPathB为小类码
string ClassPathC = this.txtClass3.Text.Trim(); // ClassPathC为三级类码
string news_ClassPath = "html/" + ClassPathA + "/" + ClassPathB + "/" + ClassPathC + "/";
//HttpContext.Current.Server.MapPath(相对路径):把相对路径转为服务器上的绝对路径。File.Exists(绝对路径):检查是否存在绝对路径指向的文件或目录。
if (!File.Exists(HttpContext.Current.Server.MapPath("html/") + ClassPathA))
{
//System.IO.Directory.CreateDirectory(文件夹绝对路径):建立绝对路径文件夹。
System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("html/") + """" + ClassPathA);
}
if (!File.Exists(HttpContext.Current.Server.MapPath("html/" + ClassPathA) + ClassPathB))
{
System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("html/" + ClassPathA) + """" + ClassPathB);
}
if (!File.Exists(HttpContext.Current.Server.MapPath("html/" + ClassPathA + "/" + ClassPathB) + ClassPathC))
{
System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("html/" + ClassPathA + "/" + ClassPathB) + """" + ClassPathC);
}
二、在添加和修改文章时,自动生成HTML文件
在添加和修改新闻文章时,可以生动生成HTML页面,也可以留待以后生成。本网站在添加或者修改一篇文章后是自动生成HTML页面的,代码如下:
//自动生成Html文件
string news_title=this.txtTitle.Text;
string strBody = Server.HtmlDecode(this.FreeTextBox1.Text.Trim());
strBody = strBody.Replace(System.Web.HttpUtility.HtmlDecode(""), ""); //去掉代替空格显示的“?”号
string news_Body = strBody;
string news_id=Request.QueryString["articleID_int"].ToString();
string news_Writer=this.txtWriter.Text;
string news_Source = this.txtsource.Text;
string news_Dateantime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
bool result = HtmlDB.WriteFile_1(news_title, news_Body, news_id, news_Writer, news_Source, news_Dateantime, news_ClassPath);
if (result)
{
Response.Write("〈script〉alert(""生成HTML页面成功"");〈/script〉");
}
else
{
}
这里的关键是黑体字部分,因为需要把HtmlDecode中的一些如""符号转换为空格,还有一些不明不白的符号,我也弄不清楚,必需要转换一下,修改时从数据库中读取也是一样需要转换。其他的代码相信大家能看懂的。
三、自动批量生成HTML页面和索引页
需要说明的是,本程序首先由新闻数据库创建了一个强类型的DataSet,以下代码均针对强类型数据集进行操作,关于强类型数据集不在本文论讨的范围之内,你可以在这里得到有关创建数据访问层和强类型的知识(http://whx.tzgt.gov.cn/newshow/newBodyShow.aspx?articleID=688),里不加表述。如果对强类型数据集不熟悉,也可以直接对数据库进行操作以获取数据。
1.创建一个HTML页面模板
创建一个名为HtmlModule.htm模板,用以统一所生成的HTML页面的格式,创建一个名叫HtmlModuleAllNews.htm的模板页面,用于生成索引页面。在创建HTML页面时系统实际上是读取该模板,然后添加替换有关内容后而生成HTML页面的。关于HTML页面模板你可以自己做一个HTML页面,由于我的HtmlModule.htm模板代码较大,所以用索引页面的模板HtmlModuleAllNews.htm示出:
〈!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"〉
〈html xmlns="http://www.w3.org/1999/xhtml" 〉
〈head〉
〈title〉{txtTitle}〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=gb2312" /〉
〈/head〉
〈body〉
〈table style="width: 800px; border-right: #666699 1px solid; border-top: #666699 1px solid; border-left: #666699 1px solid; border-bottom: #666699 2px solid;"〉
〈tr〉
〈td align="center" style="font-size: 24px; color: #ffffff; height: 60px; background-color: #669999"〉
{txtTitle}
〈/td〉
〈/tr〉
〈tr〉
〈td style="background-color: #e3ffff"〉
〈br /〉
{content}
〈/td〉
〈/tr〉
〈tr〉
〈td style="height: 80px; background-color: #99cc99" align="center"〉
〈span style="color: #003366"〉泰州市国土资源局〈/span〉 〈span style="color: #ff0066"〉王宏喜〈br /〉
〈span style="color: #660033"〉地址〈/span〉〈span style="color: #000000"〉:〈/span〉〈span
style="color: #000066"〉泰州市凤凰东路号〈/span〉〈span style="color: #000000"〉 〈/span〉
〈span style="color: #990066"〉邮编〈/span〉〈span style="color: #000000"〉:〈br /〉
〈span style="color: #990066"〉电话〈/span〉:〈span style="color: #000066"〉0523-6883033 电子邮件〈/span〉:〈span
style="color: #0000ff"〉Tzgtwhx@163.com 〈span style="color: #ff0066"〉QQ:〈/span〉〈/span〉〈/span〉〈/span〉〈/td〉
〈/tr〉
〈/table〉
〈/body〉
〈/html〉
注意,加黑地方的{txtTitle}、{content}就是需要替换的内容。
2.创建一个自动生成Html页面的的公共类
创建一个名叫HtmlDB.cs的公共类,它的作用主要是生成Html页面和索引页面。其类代码如下:
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.Text;
using System.IO;
///
/// DB 的摘要说明
///
public class HtmlDB
{
public HtmlDB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//以下WriteFile_1()用于生成HTML页面
public static bool WriteFile_1(string news_title, string news_Body, string news_id, string news_Writer, string news_Source, string news_Dateantime, string news_ClassPath)
{
//申明字符编码,命名空间System.Text
Encoding encoding = Encoding.GetEncoding("gb2312");
//读取模板文件
string TemplatePath = HttpContext.Current.Server.MapPath("HtmlModule.htm");//
//StreamReader实现一个 TextReader,使其以一种特定的编码从字节流中读取字符,命名空间System.IO。
StreamReader sr = null;
//StreamWriter实现一个 TextWriter,使其以一种特定的编码向流中写入字符,命名空间Syste.IO。
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(TemplatePath, encoding);
//从流的当前位置到末尾读取流
str = sr.ReadToEnd();
}
catch (Exception e1)
{
HttpContext.Current.Response.Write(e1.Message);
HttpContext.Current.Response.End();
}
finally
{
sr.Close();
sr.Dispose();
}
//生成路径
//string myPath = "html/";
string myPath = news_ClassPath;
string OutPutPath = HttpContext.Current.Server.MapPath(myPath);
//生成的文件名
//string newName = DateTime.Now.ToString("yyyyMM_") + news_id + ".html";
string newName = news_id + ".html";
//替换内容
str = str.Replace("{title}", news_title);
str = str.Replace("{content}", news_Body);
str = str.Replace("{txtTitle}", news_title);
str = str.Replace("{txtWriter}", news_Writer);
str = str.Replace("{txtSource}", news_Source);
str = str.Replace("{txtDateantime}", news_Dateantime);
//str = str.Replace("{newID}", news_id);
//写文件
try
{
sw = new StreamWriter(OutPutPath + newName,false, encoding);
sw.Write(str);
//清理当前编写器的所有缓存区,并将缓存数据写入基础流。
sw.Flush();
}
catch (Exception e2)
{
HttpContext.Current.Response.Write(e2.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
sw.Dispose();
}
return true;
}
//以下WriteFileAllNew()用于生成HTML索引页
public static bool WriteFileAllNew(string fileTitle, string fileBody, string filePath, string FileName)
{
//申明字符编码,命名空间System.Text
Encoding encoding = Encoding.GetEncoding("gb2312");
//读取模板文件
string TemplatePath = HttpContext.Current.Server.MapPath("HtmlModuleAllNews.htm");//
//StreamReader实现一个 TextReader,使其以一种特定的编码从字节流中读取字符,命名空间System.IO。
StreamReader sr = null;
//StreamWriter实现一个 TextWriter,使其以一种特定的编码向流中写入字符,命名空间Syste.IO。
StreamWriter sw = null;
string str = "";
try
{
sr = new StreamReader(TemplatePath, encoding);
//从流的当前位置到末尾读取流
str = sr.ReadToEnd();
}
catch (Exception e1)
{
HttpContext.Current.Response.Write(e1.Message);
HttpContext.Current.Response.End();
}
finally
{
sr.Close();
sr.Dispose();
}
//生成路径
//string myPath = "html/";
string myPath = filePath;
string OutPutPath = HttpContext.Current.Server.MapPath(myPath);
//生成的文件名
//string newName = DateTime.Now.ToString("yyyyMM_") + news_id + ".html";
string newName = FileName + ".html";
//替换内容
str = str.Replace("{txtTitle}", fileTitle);
str = str.Replace("{content}", fileBody);
//写文件
try
{
sw = new StreamWriter(OutPutPath + newName, false, encoding);
sw.Write(str);
//清理当前编写器的所有缓存区,并将缓存数据写入基础流。
sw.Flush();
}
catch (Exception e2)
{
HttpContext.Current.Response.Write(e2.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
sw.Dispose();
}
return true;
}
}
以上代码主要有两个函数,一个是WriteFile_1()用于生成HTML页面,另一是WriteFileAllNew()用于生成HTML索引页。WriteFile_1()共有七个参数,其中:
news_title:用于替换HTML页面的标题;
news_Body:用于替换HTML页面的正文
news_id:为文章的ID号;
news_Writer:用于替换HTML页面的作者姓名;
news_Source:用于替换HTML页面的文章来源;
news_Dateantime:用于替换HTML页面的时间;
news_ClassPath:为存储HTML页面文件的路径;
关于WriteFileAllNew()参数基本与WriteFile_1()参数含义相似,不多叙述。
3.创建生成Html页的Web页面
本系统中将生成Html页的Web页面取名为aspxToHtml.aspx,页面视图如下,主要由三个DropDownList控件和五个Button控件组成,三个DropDownList控件用于选择新闻分类,五个Button控件用于选择自动生成的类型。如选择生成“大类”,则自动生成这一大类的所有Html页,并自动生成这一大类的索引页,以此类推。如果选择“全部”,则生成本网站的所有新闻的Html页,并生成一个总的索引页。

aspxToHtml.aspx页的后台代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;
using System.IO;
public partial class newOperate_aspxTohtml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.Button5.Attributes.Add("Onclick", "window.close();"); //在本地关闭当前页,而不需要发送到服务器去关闭当前页时
if (!Page.IsPostBack)
{
//自动检索二级类
SqlConnection con = db.createconnection();
con.Open();
SqlCommand cmd = new SqlCommand("select classID_2,classname from Menulclass_2where classID=1", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlnewclass2.DataSource = sdr;
this.ddlnewclass2.DataTextField = "classname";
this.ddlnewclass2.DataValueField = "classID_2";
//classID_b = (int)(this.ddlnewclass2.DataValueField);
this.ddlnewclass2.DataBind();
con.Close();
//自动检索三级类
SqlConnection con1 = db.createconnection();
con1.Open();
SqlCommand cmd1 = new SqlCommand("select classID_3,classname from Menulclass_3where classID_2=11", con1);
SqlDataReader sdr1 = cmd1.ExecuteReader();
this.ddlnewclass3.DataSource = sdr1;
this.ddlnewclass3.DataTextField = "classname";
this.ddlnewclass3.DataValueField = "classID_3";
this.ddlnewclass3.DataBind();
con1.Close();
}
}
protected void ddlnewclass_SelectedIndexChanged(object sender, EventArgs e)
{
//当选择了大类按钮,则自动填充小类的内容
this.ddlnewclass2.Items.Clear();
SqlConnection con = db.createconnection();
con.Open();
//Response.Write(this.ddlnewclass.SelectedItem.Value);
SqlCommand cmd = new SqlCommand("select classID_2,classname from Menulclass_2where classID=" + this.ddlnewclass1.SelectedValue, con);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlnewclass2.DataSource = sdr;
this.ddlnewclass2.DataTextField = "classname";
this.ddlnewclass2.DataValueField = "classID_2";
this.ddlnewclass2.DataBind();
con.Close();
//自动填充三级类的内容
this.ddlnewclass3.Items.Clear();
SqlConnection conn = db.createconnection();
conn.Open();
//Response.Write(this.ddlnewclass2.SelectedItem.Value);
SqlCommand cmdn = new SqlCommand("select classID_3,classname from Menulclass_3where classID_2=" + this.ddlnewclass2.SelectedValue, conn);
SqlDataReader sdrn = cmdn.ExecuteReader();
this.ddlnewclass3.DataSource = sdrn;
this.ddlnewclass3.DataTextField = "classname";
this.ddlnewclass3.DataValueField = "classID_3";
this.ddlnewclass3.DataBind();
conn.Close();
}
protected void ddlnewclass2_SelectedIndexChanged(object sender, EventArgs e)
{
//当选择了小类按钮,则自动填充三级类的内容
this.ddlnewclass3.Items.Clear();
SqlConnection conn = db.createconnection();
conn.Open();
//Response.Write(this.ddlnewclass2.SelectedItem.Value);
SqlCommand cmdn = new SqlCommand("select classID_3,classname from Menulclass_3where classID_2=" + this.ddlnewclass2.SelectedValue, conn);
SqlDataReader sdrn = cmdn.ExecuteReader();
this.ddlnewclass3.DataSource = sdrn;
this.ddlnewclass3.DataTextField = "classname";
this.ddlnewclass3.DataValueField = "classID_3";
this.ddlnewclass3.DataBind();
conn.Close();
}
protected void ddlnewclass3_SelectedIndexChanged(object sender, EventArgs e)
{
//this.BindToGridViewBody();//调用下面的BindToGridViewBody()绑定数据
}
public static bool htmlMapPath(string ClassPathA, string ClassPathB, string ClassPathC)
{
margin: 0cm
分享到:
相关推荐
7. **批量生成**:对于大型网站,手动生成所有页面可能不切实际。可以编写脚本或工具,遍历所有可能的URL,自动生成对应的静态HTML页。 8. **维护与更新**:当内容更改时,必须更新对应的静态页。可以设置触发器,...
"ASP.Net自动批量生成HTML页面和索引页.doc"可能介绍的是如何自动化生成静态HTML页面,这对于提升网站性能和SEO友好性很有帮助。ASP.NET提供了多种方法来生成静态内容,如Response.Write、Server.Execute或者使用...
2. 并行处理:批量生成大量页面时,考虑使用多线程或异步处理,提高效率。 3. 部署策略:在负载均衡的服务器集群中,确保所有节点同步静态内容。 综上所述,.NET框架提供了丰富的工具和技术,支持高效、灵活地生成...
2. **自动生成**:在ASP.NET应用中,可以编写自定义的HttpHandler或HttpModule,在页面请求时实时生成HTML并缓存,下次请求时直接返回缓存的HTML,实现类似静态的效果。 3. **工具自动化**:有一些第三方工具或库,...
本文将深入探讨如何通过.NET后台批量生成静态HTML页面,并讨论其重要性和实施步骤。 一、HTML静态页面的优势 1. 加速加载:与动态页面相比,静态HTML页面直接由服务器发送到浏览器,无需执行服务器端的脚本,因此...
在ASP.NET中,Web应用程序由一系列的.aspx页面构成,每个页面都是一个包含HTML、服务器控件和脚本的混合文件。服务器控件如TextBox、Button等,可以处理用户交互并生成HTTP响应。此外,ASP.NET还支持Code-behind模式...
ASP.NET是一种由微软公司推出的用于构建Web应用程序的开发框架,它基于.NET Framework,提供了丰富的功能和强大的性能,是开发高效、安全的网络应用的理想选择。本文将深入探讨如何利用ASP.NET技术构建一个功能完备...
ASP.NET学生成绩系统是一个基于.NET框架开发的学生管理系统,主要用于记录和管理学生的成绩信息。这个系统的核心功能包括但不限于数据录入、查询、统计分析以及报告生成。以下是对该系统的详细解析: 1. **学号设计...
5. **脚本生成**:能够自动生成 SQL 脚本,如创建表、索引、视图和存储过程的脚本。 6. **权限管理**:允许管理员分配不同用户的访问权限,控制对数据库对象的操作范围。 **四、ASP.NET 技术实现** 1. **MVC 架构**...
2. **事件驱动模型**:ASP.NET页面生命周期基于事件,用户交互触发服务器端事件,使得页面逻辑更易于管理和调试。 3. **控件基础**:ASP.NET提供丰富的服务器控件,如按钮、文本框等,这些控件简化了界面开发,并且...
### ASP.NET导入CSV文件到数据库:深入解析与实践 在当今数据驱动的环境中,处理大量数据集成为软件开发中的常见需求。CSV(Comma Separated Values)文件因其简单且易于生成和读取的特点,在数据交换和存储方面被...
这里我们将深入探讨如何在C# ASP.NET环境中实现这一功能,同时也会涉及到SQL数据库操作和DBA(数据库管理员)的角色。 首先,静态页生成的主要目的是提高网站的加载速度,因为它减少了服务器动态处理请求的需求。当...
综上所述,ASP.NET批量更新涉及多种技术和策略,包括但不限于ADO.NET、Entity Framework、Dapper等,开发者可以根据项目需求和性能要求选择合适的方法。批量更新不仅提高了效率,还能有效管理资源,是大型数据操作的...
ASP.NET (C#) 生成Excel并导出Excel是一个常见的需求,在Web应用中,用户可能需要将数据库中的数据导出为Excel格式以便于分析或离线处理。在.NET框架下,我们可以利用C#语言和微软提供的COM互操作库Microsoft.Office...
【网上图书管理系统 ---ASP.NET】是一个基于ASP.NET技术构建的在线购物平台,专门用于图书的选购和管理。ASP.NET是微软公司推出的Web应用程序框架,它为开发者提供了强大的工具和库,使得构建动态、数据驱动的网站变...
在IT领域,C#、SQLite和ASP.NET是三个非常重要的技术组件,它们分别在不同的层面上服务于软件开发,特别是Web应用程序的构建。本篇将详细阐述这三个技术的结合及其在实际应用中的作用。 首先,C#(读作"C Sharp")...
【ASP.NET网上书店项目概述】 ASP.NET是一种由微软公司开发的服务器端Web应用程序框架,用于构建动态、数据驱动的Web应用程序。在这个“网上书店”项目中,开发者利用ASP.NET技术构建了一个全面的在线图书销售平台...
它提供了丰富的服务器控件、事件驱动模型以及自动页面状态管理,使得开发者能够更高效地创建动态网页和Web服务。 2. **系统架构** ASP.NET物流管理系统可能采用了三层架构设计,包括表示层(UI)、业务逻辑层(BLL...
同时,ASP.NET的页面生命周期管理使开发者能有效地控制页面从请求到响应的各个阶段,确保数据的正确处理和页面的高效运行。 其次,C#作为.NET框架的主要编程语言,它具有面向对象的特性,支持类、接口、继承等概念...
ASP.NET中的Repeater控件是一种非常灵活的数据呈现控件,常用于动态生成HTML表格或其他HTML结构,以展示来自数据源的数据。本篇文章将深入探讨Repeater控件的使用,包括数据原样导出和动态显示/隐藏Repeater中的列。...