唐山网站建设

设为主页 加入收藏 繁體中文

ASP.NET天生静态HTML页面并分别按年月目录寄存

核心提示:ASP.NET天生静态HTML页面并分别按年月目录寄存

1说到新闻系统的话,1定会谈到静态页面天生的,由于静态页面不但是读取速度快,而且又安全;

静态页面的天生不论是小到现在的企业网站大至网易,QQ等门户都用到了;

那末我们如何来天生静态页呢?

以甚么方式天生静态页面呢……

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

静态页面命名

同1寄存目录

静态页面模板

页面天生

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

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

命名:在天生的时候1般来讲是采取新闻发布的时间转换成的字符串,这个是不会重复的。另外我还按年份月份把这些静态文件寄存在不同的目录,以便于治理,在这里根据1个新闻的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()
        ...{
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        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;
        }
    }
}

唐山网站建设www.fw8.net


TAG:模板,内容,文件,页面,静态
评论加载中...
内容:
评论者: 验证码: