生成静态页面怎么做啊?不是一般的急啊

nuonuo33 2011-01-27 10:58:58


我想点击一下“生成HTML” 这个公告的内容就自动变成 **********(*号记录为当前时间,精确到秒).html 的格式

后台页面代码:
        //判断是否生成静态页面
public string isHtml(string html, int id)
{
if (string.IsNullOrEmpty(html))
{
return "<a href='JstaticPost.aspx?nid=" + id + "'>生成HTML</a>";
}
else
{
return html;
}
}


前台页面代码:

<td><%# isHtml(Eval("HtmlAddress").ToString(), Convert.ToInt32(Eval("NewsInfoId")))%></td>



这是生成静态页面跳转的页面:

public partial class JstaticPost : System.Web.UI.Page
{
string uid = string.Empty;
string title = string.Empty;
string cont = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Cookies["nid"] == null)
{
Response.Redirect("Login.aspx");
}
else
{
if (Request["nid"] != null)
{
uid = Request["nid"].ToString();
}
else
{
Response.Redirect("JUser.aspx");
}
if (!IsPostBack)
{
GetTC();
Pub(title, cont);
}
}
}
//获取公告的新闻和标题
private void GetTC()
{
string getSqls = "select NewsTitle,NewsContent from NewsInfo where NewsInfoId="+uid;
SqlDataReader dr = DbSqlHelper.ExecuteReader(getSqls);
try
{
if (dr.Read())
{
title = dr["NewsTitle"].ToString();
cont = dr["NewsContent"].ToString();
}
}
catch
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('对不起,功能正在维护中........');</script>");
}
finally
{
dr.Close();
}
}
//生成静态页面
private void Pub(string title,string con)
{
FileStream fs = new FileStream(Server.MapPath("HTML/News.htm"),FileMode.Open);
StreamReader sr = new StreamReader(fs);
string str = sr.ReadToEnd();
sr.Close();
//替换静态页面的内容
str = str.Replace("{ping:title}",title);
str = str.Replace("{ping:content}",con);
//1
string filename = DateTime.Now.ToString("yyyyMMddhhmmss")+".htm";
string path = Server.MapPath("/NewsHtml/")+filename;
StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.Default);
sw.Write(str);
sw.Close();
//将静态页面保存在数据库
InDB(filename);
Response.Redirect("Jpost.aspx");
}
//将静态页面生成的名称存入数据库中
private void InDB(string ht)
{
string InDBsql = "update NewsInfo set HtmlAddress='"+ht+"' where NewsInfoId="+uid+"";
DataSet ds = DbSqlHelper.ExecuteDataSet(InDBsql);

}


但是就不能显示- -
...全文
234 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
双烟弥勒 2011-01-27
  • 打赏
  • 举报
回复
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Text.RegularExpressions;
/// <summary>
///PageHelper 的摘要说明
/// </summary>
public static class PageHelper
{

/// <summary>
/// 生成静态页
/// </summary>
/// <param name="HtmlName"></param>
/// <param name="strId"></param>
public static void GenarateHtml(string HtmlName, string strId)
{
string strPath = "~/Articles/" + HtmlName.Substring(0, HtmlName.IndexOf('/'));
if (!Directory.Exists(HttpContext.Current.Server.MapPath(strPath)))
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(strPath));

string filename = HttpContext.Current.Server.MapPath("~/Articles/" + HtmlName);
StringWriter sw = new StringWriter();
try
{
HttpContext.Current.Server.Execute("~/ArticleView.aspx?tgb=" + strId, sw);
}
catch { }
string content = sw.ToString();

try
{
using (FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write))
{
using (StreamWriter streamwriter = new StreamWriter(fs, HttpContext.Current.Response.ContentEncoding))
{
streamwriter.Write(content);
}
}
}
finally
{

}
}

/// <summary>
/// 设置文本编辑器的 路径
/// </summary>
/// <param name="d"></param>
public static void SetEditor(CuteEditor.Editor _editor)
{
//每个月换个目录
string strPath = HttpContext.Current.Server.MapPath("~/FilesUpload/Files/"+System.DateTime.Now.ToString("yyyy-MM"));
if (!System.IO.Directory.Exists(strPath))
{
System.IO.Directory.CreateDirectory(strPath);
}
_editor.SetSecurityGalleryPath(strPath);
}


public static void SetCss(Page page,string href)
{
HtmlLink link = new HtmlLink();
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
link.Href = href;
page.Header.Controls.Add(link);

}


public static string CutTitle(string title, int len)
{
if(title.Length>len)
return title.Substring(0,len)+"...";
else
return title;
}

public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "",
RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();

return Htmlstring;
}


}
fendouaj 2011-01-27
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;


private Thread thr;

public Thread Thr
{
get
{
if (System.Web.HttpContext.Current.Application["thread"] == null)
return null;
else
return (Thread)System.Web.HttpContext.Current.Application["thread"];
}
set
{
System.Web.HttpContext.Current.Application["thread"] = value;
}
}

public CreateHTML() { }

/// <summary>
/// 生成静态页
/// </summary>
/// <param name="reqpath">请求地址</param>
/// <param name="savepath">生成路径</param>
public CreateHTML(string reqpath, string savepath)
{
if (thr == null)
{
ThreadStart starter = delegate { GenerateHtml(reqpath, savepath); };
Thread th = new Thread(starter);
th.IsBackground = false;
thr = th;
thr.Start();
}
else
{
if (thr.ThreadState.Equals(ThreadState.Aborted))
Thread.ResetAbort();
}
}

/// <summary>
/// 批量生成
/// </summary>
/// <param name="dic">key是生成地址,value是请求地址</param>
public CreateHTML(Dictionary<string,string> dic)
{
if (thr == null)
{
ThreadStart starter = delegate { GenerateHtml(dic); };
Thread th = new Thread(starter);
th.IsBackground = false;
thr = th;
thr.Start();
}
else
{
if (thr.ThreadState.Equals(ThreadState.Aborted))
Thread.ResetAbort();
}
}




#region 生成静态页

/// <summary>
/// 生成静态页
/// </summary>
/// <param name="reqpath">请求地址</param>
/// <param name="savepath">生成地址</param>
public bool BulidHtml(string reqpath, string savepath)
{
bool reslut = false;
try
{
WebRequest wr = WebRequest.Create(reqpath);
WebResponse wrs = wr.GetResponse();
Stream str = wrs.GetResponseStream();
StreamReader sr = new StreamReader(str, System.Text.Encoding.GetEncoding("utf-

8"));
string abc = sr.ReadToEnd();

StreamWriter sw = null;

string path = savepath.Replace("/", @"\");

int pos = path.LastIndexOf(@"\");
if (pos != -1)
{
string dir = path.Substring(0, pos);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
try
{
sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-

8"));
sw.Write(abc);
reslut = true;
}
catch (Exception ex)
{
//throw new Exception("出错啦:" + ex.Message);
}
finally
{
wrs.Close();
str.Close();
str.Flush();
sr.Close();
sw.Flush();
sw.Close();
}
}
return reslut;
}
catch (Exception)
{

return reslut;
}

}


/// <summary>
/// 生成静态页
/// </summary>
/// <param name="reqpath">请求地址</param>
/// <param name="savepath">生成地址</param>
private void GenerateHtml(string reqpath, string savepath)
{
WebRequest wr = WebRequest.Create(reqpath);
WebResponse wrs = wr.GetResponse();
Stream str = wrs.GetResponseStream();
StreamReader sr = new StreamReader(str, System.Text.Encoding.GetEncoding("utf-8"));
string abc = sr.ReadToEnd();

StreamWriter sw = null;

string path = savepath.Replace("/", @"\");

int pos = path.LastIndexOf(@"\");
if (pos != -1)
{
string dir = path.Substring(0, pos);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
try
{
sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-8"));
sw.Write(abc);
}
catch (Exception ex)
{
//todo:此处单独做错误处理
}
finally
{
wrs.Close();
str.Close();
str.Flush();
sr.Close();
sw.Flush();
sw.Close();
thr.Abort();
}
}
}

/// <summary>
/// 批量生成静态页
/// </summary>
/// <param name="dic">key是生成地址,value是请求地址</param>
private void GenerateHtml(Dictionary<string,string> dic)
{

foreach (string savepath in dic.Keys)
{
string reqpath = dic[savepath];
WebRequest wr = WebRequest.Create(reqpath);
WebResponse wrs = wr.GetResponse();
Stream str = wrs.GetResponseStream();
StreamReader sr = new StreamReader(str, System.Text.Encoding.GetEncoding("utf-

8"));
string abc = sr.ReadToEnd();

StreamWriter sw = null;

string path = savepath.Replace("/", @"\");

int pos = path.LastIndexOf(@"\");
if (pos != -1)
{
string dir = path.Substring(0, pos);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
try
{
sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-

8"));
sw.Write(abc);
}
catch (Exception ex)
{
//todo:此处单独做错误处理
}
finally
{
wrs.Close();
str.Close();
str.Flush();
sr.Close();
sw.Flush();
sw.Close();
}
}
}
thr.Abort();
}
#endregion

nuonuo33 2011-01-27
  • 打赏
  • 举报
回复

帮帮忙吧,卡住了
nuonuo33 2011-01-27
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 beckapi 的回复:]
文件生成了吗? 生成的文件源代码是什么...
[/Quote]
没有生成
nuonuo33 2011-01-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wuyq11 的回复:]
静态页
protected override void Render(HtmlTextWriter writer) {
StreamWriter r=new StreamWriter(Server.MapPath(""), false,System.Text.Encoding.UTF8);
HtmlTextWriter h=new HtmlTextWriter(r);
……
[/Quote]
还是没反应,麻烦能否说的详细一点,可以吗?
BeckLikeCoding 2011-01-27
  • 打赏
  • 举报
回复
文件生成了吗? 生成的文件源代码是什么...
wuyq11 2011-01-27
  • 打赏
  • 举报
回复
静态页
protected override void Render(HtmlTextWriter writer) {
StreamWriter r=new StreamWriter(Server.MapPath(""), false,System.Text.Encoding.UTF8);
HtmlTextWriter h=new HtmlTextWriter(r);
base.Render(h);
r.Close();
h.Close();
}
StringWriter wr = new StringWriter();
Server.Execute("", wr);
this.lit.Text = Server.HtmlEncode(wr.ToString());
File.WriteAllText(Server.MapPath(""), wr.ToString());

nuonuo33 2011-01-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lw402365015 的回复:]
帮顶 。。。
[/Quote]
谢谢
nuonuo33 2011-01-27
  • 打赏
  • 举报
回复


不好意思 ,图片长了,就是这个“生成HTML”这个按钮
BeckLikeCoding 2011-01-27
  • 打赏
  • 举报
回复
用我这个吧

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;

/// <summary>
/// 动态生成html页面的类
/// </summary>
public static class DymicHtml
{

public static string GoldCreateHtml(HttpServerUtility server, string title, string addTime, string contents)
{
Random rd = new Random();
string url = DateTime.Now.ToString("yyyyMMddHHmmss") + rd.Next(1000, 9999);//保存动态生成的页面的名字
string[] newContent = new string[5];//定义和html标记数目一致的数组
StringBuilder strhtml = new StringBuilder();
try
{
//创建StreamReader对象
//解决模板乱码问题
// System.IO.StreamReader m_fs = new System.IO.StreamReader(Hfile_SelectFile.Value, System.Text.Encoding.GetEncoding("gb2312"));
//using (StreamReader sr = new StreamReader(server.MapPath("~/") + "Gold\\MgrSpdb\\common\\GoldNewsListTemplate.html", Encoding.GetEncoding("gb2312")))
using (StreamReader sr = new StreamReader(server.MapPath("~/") + "common\\HotFinacial.html", Encoding.GetEncoding("gb2312")))
{
string oneline;
//读取指定的HTML文件模板
while ((oneline = sr.ReadLine()) != null)
{
strhtml.Append(oneline);
}
sr.Close();
}
}
catch (Exception err)
{
//输出异常信息
Console.WriteLine(err.ToString());
}
//为标记数组赋值
newContent[0] = title;//网页标题
newContent[1] = title;//正文标题
newContent[2] = addTime;
newContent[3] = contents;
newContent[4] = url;

//根据上面新的内容生成html文件
try
{
//若目录不存在,则新建目录
string dir = server.MapPath("~/") + "c\\" + DateTime.Now.ToString("yyyyMMdd");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
//指定要生成的HTML文件
//string fname = server.MapPath("~/") + "Gold\\MgrSpdb\\c\\" + url + ".html";
string fname = dir+"\\"+ url + ".html";
//替换html模版文件里的标记为新的内容
for (int i = 0; i < 5; i++)
{
strhtml.Replace("$htmlkey[" + i + "]", newContent[i]);
}
//创建文件信息对象
FileInfo finfo = new FileInfo(fname);
//以打开或者写入的形式创建文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
//把新的内容写到创建的HTML页面中
sw.WriteLine(strhtml);
sw.Flush();
sw.Close();
}
//设置超级链接的属性
}
catch (Exception err)
{
Console.WriteLine(err.ToString());
}
return url;
}


}

huangwenquan123 2011-01-27
  • 打赏
  • 举报
回复

//简单写了下!
string opath = "html.htm";
string path = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + ".html";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(Server.MapPath(opath), Encoding.UTF8))
{
sb.Append(sr.ReadToEnd().Replace("${title}", "csdn标题").Replace("${content}", "csdn内容"));
}
using (StreamWriter sw = new StreamWriter(Server.MapPath(path),true, Encoding.UTF8))
{
sw.Write(sb.ToString());
}


<!--html.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>无标题页</title>
</head>
<body>
${title}
${content}
</body>
</html>

62,072

社区成员

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

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

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

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