.net页面静态化

kk297173176 2010-05-12 12:02:01
如题 求一小例子。qq:1063080142 发我邮箱里就行了

不要片段 ,给一个完整的例子
...全文
262 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuyq11 2010-05-12
  • 打赏
  • 举报
回复
public static bool WriteFile(string strText,string strContent,string strAuthor)
{
string path = HttpContext.Current.Server.MapPath("/news/");
Encoding code = Encoding.GetEncoding("gb2312");
string temp = HttpContext.Current.Server.MapPath("/news/template.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();
}

public static string getUrltoHtml(string Url)
{
errorMsg = "";
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
System.Net.WebResponse wResp =wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
return reader.ReadToEnd();
}
catch(System.Exception ex)
{
errorMsg = ex.Message ;
}
return "";
}
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();
}
mngzilin 2010-05-12
  • 打赏
  • 举报
回复
重写Page类的Render方法:

protected override void Render(HtmlTextWriter writer)
{
StringWriter sw= new StringWriter();
base.Render(new HtmlTextWriter(sw));//获取正常输出的html

string html = sw.ToString();
writer.WriteLine(html.Trim());//输出过滤后的html

}
ck11926375 2010-05-12
  • 打赏
  • 举报
回复
/**////
/// 此处为获取静态页模板,然后用新数据替换
///
/// 新闻标题
/// 新闻内容
/// 页码连接格式
/// 静态页名称
public void WriteFile(string strText, string content, string page, string name)
...{
string path = HttpContext.Current.Server.MapPath("news/get");
System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312");

// 读取模板文件
string temp = HttpContext.Current.Server.MapPath("news/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 =name.ToString();
// 替换内容
// 这时,模板文件已经读入到名称为str的变量中了
str = str.Replace("$title", strText); //模板页中的$title,即标题
str = str.Replace("$content", content);//模板页中的$content,即内容
str = str.Replace("$page", page);//模板页中的$page,即页码连接格式

// 写文件
try
...{
sw = new StreamWriter(path + "/"+htmlfilename, false, code);
sw.Write(str);
sw.Flush();
}
catch (Exception ex)
...{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
...{
sw.Close();
}
}
}


以下为模板页test.html的代码
<!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>
<div style="font-size: 20px; text-align: center;">文章分页测试</div>
<div style="font-size: 15px; text-align: center; color: #009966;">$title</div>
<div style="font-size: 12px; text-align: center; color: darkgray;">$page</div>
<div style="font-size: 12px;">$content</div>


</body>
</html>
nosuchtracter 2010-05-12
  • 打赏
  • 举报
回复
www.51aspx.com
上面很多新闻系统,就是静态。
nosuchtracter 2010-05-12
  • 打赏
  • 举报
回复
看看CMS
或者用google搜索下
风骑士之怒 2010-05-12
  • 打赏
  • 举报
回复
伪静态 ,或 生成静态页
Vedy 2010-05-12
  • 打赏
  • 举报
回复
urlrewriter.dll 配置好就可以了 很简单 自己找方法 伪静态很简单的
「已注销」 2010-05-12
  • 打赏
  • 举报
回复
学习、、、、

62,046

社区成员

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

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

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

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