生成静态页面问题,请大家帮忙看下
为什么生成的静态页面是空的,什么内容都没有,连html代码都没,请帮我看看哪有问题,谢了。
这是模板
- HTML code
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>{##title##} </title>
<link rel="stylesheet" type="text/css" href="../../css/css.css" />
<link rel="stylesheet" type="text/css" href="../../css/index.css" />
</head>
<body>
<div style="width:1002px; margin:auto;">
<iframe scrolling="no" width="1002px" height="132px" src="../../UserControl/newsheader.aspx" frameborder="0"> </iframe> </div>
<div style="width:1002px; margin:auto;"> <img src="../../images/xwzx_02.gif" /> </div>
<div id="newsshow">
<div class="news_nr">
<div style="font-family:'宋体'; font-size:16px; font-weight:bold; color:#FFFFFF; text-align:center">{##title##} </div>
<div style="font-family:'宋体'; margin-top:6px; margin-bottom:6px; font-size:12px; color:#FFFFFF; text-align:center">{##time##} </div>
<div style="font-family:'宋体'; margin-top:6px; margin-bottom:6px; font-size:12px; color:#FFFFFF; margin:6px 200px;">{##text##} </div>
</div>
</div>
<div id="Banquan"> ©2007-2008 </div>
</body>
</html>
这是生成静态页的类
- C# code
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.IO;using System.Text;/// <summary>/// 生成静态页面类/// </summary>public class StaticHtml{ /// <summary> /// 新闻标题 /// </summary> private string _title = "这里是标题的内容"; /// <summary> /// 新闻内容 /// </summary> private string _content = "暂无内容"; /// <summary> /// 新闻上传时间 /// </summary> private string _time = ""; /// <summary> /// 生成的文件的保存路径; /// </summary> private string _path = "htmlpath"; /// <summary> /// 返回静态页的地址 /// </summary> private string _htmlPath; /// <summary> /// 返回消息 /// </summary> private string _msg; /// <summary> /// 新闻标题 /// </summary> public string Title { set { _title = value; } get { return _title; } } /// <summary> /// 新闻内容 /// </summary> public string Content { set { _content = value; } get { return _content; } } /// <summary> /// 新闻上传时间 /// </summary> public string Time { set { _time = value; } get { return _time; } } /// <summary> /// 生成的文件的保存路径; /// </summary> public string Path { set { _path = value; } get { return _path; } } /// <summary> /// 返回消息 /// </summary> public string Msg { get { return _msg; } } /// <summary> /// 返回静态页的地址 /// </summary> public string HtmlPath { get { return _htmlPath; } } public StaticHtml() { // // TODO: 在此处添加构造函数逻辑 // } public void GetHtml() { //创建静态页文件夹的文件夹名(eg:2008-09) string _dt = DateTime.Now.ToString("yyyyMM"); //创建静态页文件夹的绝对路径 string _outPutPath = HttpContext.Current.Server.MapPath("~")+"\\" + Path + "\\" + _dt + "\\"; //用于创建,移动,枚举目录的实例 DirectoryInfo directoryinfo = new DirectoryInfo(_outPutPath); //如果文件夹不存在创建文件夹 if (!directoryinfo.Exists) { directoryinfo.Create(); } //编码格式 Encoding encoding = Encoding.GetEncoding("gb2312"); //静态页母版的绝对路径 string _modelPath = HttpContext.Current.Server.MapPath("Temp/temp.html"); //声明一个读流 StreamReader sr = null; //声明一个写流 StreamWriter sw = null; //声明一个字符串用来替换title,content,time string _str=""; try { //实例化一个读流对象,参数母版绝对路径和编码格式 sr = new StreamReader(_modelPath, encoding); //从当前位置到末尾把母版读到流里 sr.ReadToEnd(); } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); sr.Close(); } //生成的静态页的文件名(把-替换成“”) string _htmlName = DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".html"; _str = _str.Replace("{##title##}", Title); _str = _str.Replace("{##time##}", Time); _str = _str.Replace("{##text##}", Content); try { sw = new StreamWriter((_outPutPath + _htmlName),false,encoding); sw.Write(_str); sw.Flush(); _msg = " 成功生成静态页面!"; _htmlPath = _dt + "\\" + _htmlName; } catch(Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); _msg = "产生错误,请重试"; } finally { sw.Close(); } }}
这是添加新闻页面的cs
- C# code
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;public partial class Admin_News_AddNews : adminpagebase{ protected void Page_Load(object sender, EventArgs e) { } protected void subButton_Click(object sender, EventArgs e) { //html obj = new html(); //obj.Neirong = this.FCKeditor1.Value; //obj.Title = this.newstitle.Text.Trim(); //obj.Time = DateTime.Now.ToLongDateString(); //obj.Path = "News"; //obj.gethtml(); StaticHtml statichtml = new StaticHtml(); statichtml.Title = this.newstitle.Text.Trim(); statichtml.Content = this.FCKeditor1.Value; statichtml.Time = DateTime.Now.ToLongDateString(); statichtml.Path = "News"; statichtml.GetHtml(); dbbase db = new dbbase(); db.insertnews(this.FCKeditor1.Value, this.newstitle.Text.Trim(), statichtml.HtmlPath); Response.Write("<script>alert('" + statichtml.Msg + "')</script>"); this.newstitle.Text = this.FCKeditor1.Value = ""; }}[解决办法]
强烈建议你改变这种生产静态页面的模式
请看:
asp.net开发网站时,用模板来生产静态页面。你有可能没用过,但不可能没听说过吧?我没用过,没写过。但听说过,甚至实现细节也听说了:写一个静态的页面,里面写好自己定义的标签,比如$title $foot等等类似的,然后在另外一个页面的后台代码CS文件里,读取该模板,然后把相应的标签替换成数据库里的数据。
这么做真的好吗?
前两篇文章:
asp.net网站生成静态页面演示示例(2)(2008-5-6 13:30:52)
asp.net网站生成静态页面演示示例(2008-5-6 12:03:18)
用模板来生产静态页面的疑问:
开发方便吗?要自定义标签,我不会,也不想学。这个标签应该没有微软提供的后期维护容易吗?以后还要改这个标签,烦啊!还不如用微软的“标签”了。
传统的最普通的asp.net网站开发就是添加一个页面,然后拖拽控件上去,然后双击写代码。这种应该是每个ASP.NET程序员必会的吧?也是最开始学习ASP.NET时用的方法。我们能不能用最简单的方式实现呢?不要去改变这种开发方式,因为很多项目开发,不是我一个人开发,也不是你一个人开发,是团队大家协作开发。那么大家都会的就是传统的开发方式,我想应该尽量的用大家都会的语言、框架、开发模式来开发。这里我不是说拒绝新知识、新技术、新的开发工具和模式。如果新的工具或方法确实效率高、成熟稳定,我想我会去学习。
我说的一种生成静态页面的方式就是前面链接里提到的第一种开发方式:asp.net网站生成静态页面演示示例,这个开发方式不需要任何人改变自己的开发习惯和思路。只需要有个人把别人开发好的程序new WebClient().DownloadFile(.....)就生产了一个静态页面,可以说是最最简单方便的生产静态页面的方法。
有兴趣的朋友可以在评论中跟我探讨该话题。欢迎拍砖!!!
[解决办法]
顶.....
[解决办法]
你这样的代码谁会来回答阿,也不说明自己可能觉得的问题在哪里,也没说明静态页面生成的原理。
难道还要我们完整地研究你这么长的代码?
[解决办法]
把主要出问题的地方先指出来。
[解决办法]
自己断点跟踪下,比较好点
[解决办法]
string _str="";
try
{
//实例化一个读流对象,参数母版绝对路径和编码格式
sr = new StreamReader(_modelPath, encoding);
//从当前位置到末尾把母版读到流里
sr.ReadToEnd();
}
你没有给_str赋值吧?
_str=sr.ReadToEnd();
就对了