高手请看net生成靓态页的怪事

billlyh 2009-09-20 07:23:37
这样一个 页 a.aspx
顺德爱得乐服装 李铁
河南封丘农科中心 余富 0000123 河南封丘
深圳市路德赛科技有限公司 林朋
福建霞浦华祥水产品加工厂 张月
启东吉莱电子有限公司 刘能
河南省紫阳纺织品有限公司 赵大地
南京宁日包装机械有限公司 吴强
浙江省云和达成工艺厂 连占
大连渤泰建材有限公司 陈豹
汕头和阳电信

点任一行,比如 '浙江省云和达成工艺厂 连占 '
跳转到详细页b.aspx,
详细页是在b.cs中经过靓态页处理的,
protected override void Render(HtmlTextWriter writer)
{
System.IO.StringWriter html = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter tw = new HtmlTextWriter(html);
base.Render(tw);
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("b.html"), false,

System.Text.Encoding.Default);
sw.Write(html.ToString());
sw.Close();
tw.Close();
Response.Redirect("b.html");
}
会自动生成html,实际上显示的是b.html

浙江省云和达成工艺厂

公司介绍 店铺类别: 玩具


联系方式

联系人: 成奎
地址: 浙江宁波天中区海阳路海阳大楼3楼3-09
邮编:
电话: 86-769-8872109
手机:
电子邮件:



问题来了,
再返回a.aspx 再点其它行,比如'汕头和阳电信 '
跳到b.html,显示的还是 '浙江省云和达成工艺厂 '的信息,

如果b.aspx不靓态化处理,点a.aspx任一行,b.aspx都能对应到其信息

哪位高手来指点一下呀
...全文
78 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
loooooove 2009-09-21
  • 打赏
  • 举报
回复
把生成的静态页面的路径保存在每篇文章的字段中,这样和话不管是动态显示还是静态显示文章都能够找到相应的内容..
loooooove 2009-09-21
  • 打赏
  • 举报
回复
看我的方法,在现实中使用的.
首先要设计几个页面的模板:如首页,二级页面,列表页面,详细页面.
然后再通过以下几个方法来实现:
//str url:模板页的路径
//GetUrlHtml(string url) 获取模板中的内容

public static string GetUrlHtml(string Url)
{
string errorMsg = "";
string PageUrl = Url;
string HtmlCode = string.Empty;
string encoding = HttpContext.Current.Response.Charset;
StreamReader reader = null;
try
{
WebRequest wReq = System.Net.WebRequest.Create(Url);

Stream respStream = wResp.GetResponseStream();
if (encoding == "utf-8")
{
reader = new StreamReader(respStream, System.Text.Encoding.GetEncoding(encoding), false);
}
else
{
reader = new StreamReader(respStream, System.Text.Encoding.GetEncoding(encoding));
}
HtmlCode = reader.ReadToEnd();
respStream.Flush();
respStream.Close();
reader.Close();
return HtmlCode;

}
catch (System.Exception ex)
{
errorMsg = ex.Message;
}
return "";
}

//按照指定的名称生成在指定的文件夹中
public static bool CreateHtmls(string HtmlCode, string FileName)
{
string FilePath = HttpContext.Current.Server.MapPath("/" + System.Configuration.ConfigurationSettings.AppSettings["ShopFolder"] + "/Html/");
//string FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html";

System.Text.Encoding Code = System.Text.Encoding.GetEncoding("UTF-8");
StreamWriter SW = null;
try
{
SW = new StreamWriter(FilePath + FileName, false, Code);
SW.Write(HtmlCode);
SW.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.Write(FileName + "页面生成出错,请重新生成");
HttpContext.Current.Response.End();
}
finally
{
SW.Close();
}
return true;
}
也可以按时间来生成静态页面,不过由于生成的速度太快,有好多文章的名称都一样的,所以还得再添加个随机数字.
public static bool CreateHtmls(string HtmlCode, string SavePath, string FileName)
{
string FilePath, StrFileName = FileName;

FilePath = HttpContext.Current.Server.MapPath("/" + SavePath);

if (StrFileName == null)
{ StrFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html"; }
else
{ StrFileName = FileName; }
System.Text.Encoding Code = System.Text.Encoding.GetEncoding("UTF-8");
StreamWriter SW = null;
try
{
SW = new StreamWriter(FilePath + StrFileName, false, Code);
SW.Write(HtmlCode);
SW.Flush();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.Write("log.innerhtml='页面生成出错,请重新生成!'");
HttpContext.Current.Response.End();
}
finally
{
SW.Close();
}
return true;
}

前台批量生成,代码调用:

public void CreateAllContentHtml(string CreateType)
{
int i = 0; string htmlcode = string.Empty; string FileHtmlName = string.Empty;
string Log = string.Empty; string StrSql = string.Empty; int RCount = 0; int CreateCount = 0;
if (CreateType == "AllRecords")
{
StrSql = "select InfoID from Article where Status=1 and Deleted=0 order by UpdateTime desc";
}
else if (CreateType == "AllUpdateRecords")
{
StrSql = "select InfoID from Article where Status=1 and Deleted=0 and DATEDIFF(day,UpdateTime,getdate())<1 order by UpdateTime desc";
}
string[,] Info = new string[100, 2];
System.Data.SqlClient.SqlDataReader dr = null;
dr = DBHelperSQL.ExecuteReader(StrSql);
while (dr.Read())
{
//FileHtmlName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + ".html";
Info[i, 0] = URL + "templates/default/NewsShow.aspx?NewsID=" + Convert.ToString(dr.GetSqlInt32(0));
Info[i, 1] = Convert.ToString(dr.GetSqlInt32(0));
FileHtmlName = "Info-" + Info[i, 1] + ".html";
htmlcode = CreateHtml.GetUrlHtml(Info[i, 0]);
if (CreateHtml.CreateHtmls(htmlcode, FileHtmlName))
{
CreateLog += "生成<a href='" + URL + "Html/" + FileHtmlName + "' target='_blank'>" + URL + "Html/" + FileHtmlName + "</a>成功!<br/>";
DBHelperSQL.ExecuteSql("update Article set HtmlUrl='" + FileHtmlName + "' where InfoID=" + Convert.ToString(dr.GetSqlInt32(0)) + "");
CreateCount++;
}
else
{ Response.Write("生成" + Info[i, 0] + "失败!<br/>"); }
//CreateLog += "文章模板:" + Info[i, 0] + "文章ID:" + Info[i, 1] + "<br/>"; 测试生成结果
//CreateLog += "update Article set HtmlUrl='" + FileHtmlName + "' where InfoID=" + Convert.ToString(dr.GetSqlInt32(0)) + "<br/>";
i++;
if (i == 100)
{ i = 0; }
RCount++;

}
log.InnerHtml = "总共:" + RCount + "篇,已经生成<font color=red>" + CreateCount + "</font>篇<br/>" + CreateLog;
dr.Close();
i = 0;
}
billlyh 2009-09-20
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
通过urlrewriter实现伪静态
连接地址为静态页地址
public bool WriteFile(string strText)
  {
  string path = HttpContext.Current.Server.MapPath("/news/");
  Encoding code = Encoding.GetEncoding("gb2312");
  string temp = HttpContext.Current.Server.MapPath("/text.html");
  StreamReader sr=null;
  StreamWriter sw=null;
  string str=""; 
  try
  {
    sr = new StreamReader(temp, code);
    str = sr.ReadToEnd();
  }
  catch(Exception exp)
  {
    HttpContext.Current.Response.Write(exp.Message);
    HttpContext.Current.Response.End();
    sr.Close();
  }
  string htmlfilename=path + DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
  str = str.Replace("title",strText);
  try
  {
    sw = new StreamWriter(htmlfilename,false,code);
    sw.Write(str);
    sw.Flush();
  }
  catch(Exception ex)
  {
    HttpContext.Current.Response.Write(ex.Message);
    HttpContext.Current.Response.End();
  }
  finally
  {
    sw.Close();
  }
  return true;
  }
  }
}

[/Quote]

楼上这个是批量生成的吗,
billlyh 2009-09-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gdjlc 的回复:]
生成b.html时先判断b.html是否已经存在了,存在就要先删除再生成...


我自己用的生成静态页方法就是在后台添加一个新闻后就生成一个静态页。表中放静态页的路径。
还可以做一个批量生成静态页的,根据新闻的id和路径批量的生成静态页...
[/Quote]

我那个就是在每个页面的.cs中放上我上面的那代码,只生成一个对应的静态页,
怎么批量生成呀,
wuyq11 2009-09-20
  • 打赏
  • 举报
回复
通过urlrewriter实现伪静态
连接地址为静态页地址
public bool WriteFile(string strText)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
string temp = HttpContext.Current.Server.MapPath("/text.html");
StreamReader sr=null;
StreamWriter sw=null;
string str="";
try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd();
}
catch(Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
}
string htmlfilename=path + DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
str = str.Replace("title",strText);
try
{
sw = new StreamWriter(htmlfilename,false,code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
return true;
}
}
}
gdjlc 2009-09-20
  • 打赏
  • 举报
回复
生成b.html时先判断b.html是否已经存在了,存在就要先删除再生成...


我自己用的生成静态页方法就是在后台添加一个新闻后就生成一个静态页。表中放静态页的路径。
还可以做一个批量生成静态页的,根据新闻的id和路径批量的生成静态页...
小_虎 2009-09-20
  • 打赏
  • 举报
回复
呵呵,靓还是静

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧