欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【ASP.NET生成静态HTML页面并分别按年月目录存放】,下面是详细的分享!
ASP.NET生成静态HTML页面并分别按年月目录存放
using System;
using System.IO;
using System.Web;
using System.Text;
namespace PowerLeader.Components
...{
/// <summary>
/// WriteTOHtml 的摘要说明。
/// </summary>
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;
}
}
}
以上所分享的是关于ASP.NET生成静态HTML页面并分别按年月目录存放,下面是编辑为你推荐的有价值的用户互动:
相关问题:怎样改变生成静态页面的目录
答:Dim fso, MyFile,filepath filepath=server.mappath("./")&"a.txt" Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile= fso.CreateTextFile(filepath) MyFile.WriteLine("这是一个测试。") MyFile.close set fdr=fso.Getfile(fi... >>详细
相关问题:asp.net mvc5 采集一个网页(solution),将内容保...
答:用 httpWebRequest 或者WebClient 去请求指定的页面,然后将返回的信息写入到文件中就可以了。如果页面有引用其他的js,css,把js和css也给爬下来就可以了 >>详细
相关问题:什么是后台动态生成HTML静态页面? 方法是什么?
答:一种可以用IO读写原页面,输出为html 还有一种是建立html模板,IO流读取模板后动态替换模板中内容,生成静态页面 >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
