All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
PHP技巧:详解phplib模板使用过程及运行原理_php资料_编程技术
delphi文件操作AssignFile过程
ASP.NET中绑定枚举类型_.net资料_编程技术
PHP+MYSQL建设网站程序需要注意两点_php资料_编程技术
C#,3.0新特性系列:隐含类型var_[Asp.Net教程]
PHP5.2.3连接,SQLSERVER,注意事项_php资料_编程技术
ASP.NET,2.0,中的,Windows,身份验证_[Asp.Net教程]
C#过滤所有HTML代码的函数_[Asp.Net教程]
关于ASP.NET编程中的嵌套If语句_.net资料_编程技术
PHP数组的定义、初始化和数组元素的显示_[PHP教程]
Reading number is top 10 pictures
The Soviet union swimsuit exposure in the 70 year3
Wild animals melee moment of life and death1
A man's favorite things8
A man's favorite things2
Ashlynn Brooke photograph of a group1
The money of more than 100 countries and regions15
Earthquake hedge common sense
去瑜伽会所面试的经过
Photographed the passion of the clients and prostitutes in the sex trade picture2
The real super beauty4
Download software ranking
Boxer's Top ten classic battle7
Boxer's Top ten classic battle10
Ashlynn Video3
Sora aoi, the maid, students' uniforms
艳兽都市
WebService在.NET中的实战应用教学视频 → 第1集
虚拟机5.5.3版
linux初级教程
Call Of Duty2
VeryCD电驴(EasyMule) V1.1.9 Build09081
delv published in(发表于) 2014/1/23 3:12:20 Edit(编辑)
ASP.NET生成静态HTML页面并分别按年月目录存放_[Asp.Net教程]

ASP.NET生成静态HTML页面并分别按年月目录存放_[Asp.Net教程]

ASP.NET生成静态HTML页面并分别按年月目录存放_[Asp.Net教程]

一说到新闻系统的话,一定会谈到静态页面生成的,因为静态页面不但是读取速度快,而且又安全;
静态页面的生成不管是小到现在的企业网站大至网易,QQ等门户都用到了;
那么我们如何来生成静态页呢?
以什么方式生成静态页面呢……


在生成静态页面的时候有那些是要注意的呢:


静态页面命名
统一存放目录
静态页面模板
页面生成


一般来说,在原来新闻系统的基础上我们可以根据GET此页面请求的内容再生成(比如:http;//www.test.com/news.aspx?id=1,GET此页面代码直接写至一个文本文件并以HTML命名即可);


在这里我所采用的是模板生成,先用DW做一个网页模板,将标题,内容等将要动态实现的内容先以Title等替换,等在生成的时候替换成新闻的内容;


命名:在生成的时候一般来说是采用新闻发布的时间转换成的字符串,这个是不会重复的
另外我还按年份月份把这些静态文件存放在不同的目录,以便于管理,
在这里根据一个新闻的ID调用方法WriteNews()给定参数ID,它就会根据此ID从数据库中读取内容,再根据静态模板页面html/test.html生成新的静态页面存放在相应年份月份的目录


好了,下面是代码:


using System;
using System.IO;
using System.Web;
using System.Text;
namespace PowerLeader.Components
...{
/**////


/// WriteTOHtml 的摘要说明。
///

public class WriteTOHtml
...{
public WriteTOHtml()
...{
//
// TOD 在此处添加构造函数逻辑
//
}


public static void WriteNews(int id)
...{
News news = new News();
News.NewsDetails newsDetails = new PowerLeader.Components.News.NewsDetails();
newsDetails = news.GetNews(id);
bool flag;
flag = WriteFile(newsDetails);
}


public static bool WriteFile(News.NewsDetails newsDetails)
...{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath("/PowerLeader/html/"+newsDetails.addtime.ToString("yyyy")+"/"+newsDetails.addtime.ToString("MM")));
string path = HttpContext.Current.Server.MapPath("../html/"+newsDetails.addtime.ToString("yyyy")+"/"+newsDetails.addtime.ToString("MM")+"/");
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("../html/text.html");
StreamReader sr = null;
StreamWriter sw = null;
string stringTempCode = "";
try
...{
sr = new StreamReader(temp, code);
stringTempCode = sr.ReadToEnd(); // 读取文件
}
catch(Exception exp)
...{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlFileName = newsDetails.addtime.ToString("yyyyMMddHHmmss") + ".html";
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
stringTempCode = stringTempCode.Replace("PageTitle","抗战OnLine官方网站...");
stringTempCode = stringTempCode.Replace("Type",newsDetails.type.ToString().Trim());
stringTempCode = stringTempCode.Replace("Author",newsDetails.author.ToString().Trim());
stringTempCode = stringTempCode.Replace("From",newsDetails.from.Trim());
stringTempCode = stringTempCode.Replace("Time",newsDetails.addtime.ToString().Trim());
stringTempCode = stringTempCode.Replace("Title",newsDetails.title.Trim());
stringTempCode = stringTempCode.Replace("Content",newsDetails.content);
// 写文件
try
...{
sw = new StreamWriter(path + htmlFileName , false, code);
sw.Write(stringTempCode);
sw.Flush();
}
catch(Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
}
return true;
}
}
}


出处:CSDN







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.