.net aspx自动生成.htm
背景:为了SEO的收录需要,在网站中需要根据不同参数将aspx文件自动转换成.htm文件
工具:vs2008
代码如下:
using System.Net;
using System.IO;
using System.Text;
using System.Web;
//生成HTML页
public static bool WriteFile(string body, string fileName)
{
string path = HttpContext.Current.Server.MapPath("user/"); --生成文件的存放目录
Encoding code = Encoding.GetEncoding("UTF-8"); --文件编码
StreamWriter sw = null;
string htmlfilename = fileName + ".htm";
// 写文件
try
{
sw = new StreamWriter(path + htmlfilename, false, code);
sw.Write(body);
sw.Flush();
}
catch
{
return false;
}
finally
{
sw.Close();
sw.Dispose();
}
return true;
}
//生成页面入口
private void CreateUserPage(string url)
{
StringBuilder ret = new StringBuilder("");
Uri uri = new Uri(url); --这里使用模板的地址,比如http://www.jyoule.com/xxx.aspx
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
Stream rspStream = rsp.GetResponseStream();
StreamReader sr = new StreamReader(rspStream, System.Text.Encoding.UTF8);
//获取数据
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
ret.Append(read, 0, count);
count = sr.Read(read, 0, 256);
}
PageUtil.WriteFile(ret.ToString(), DateTime.Now.Ticks.ToString()); //按时间生成页面名字
}
做个广告,谢谢:
交友乐www.jyoule.com是一个最新的免费婚恋交友平台,使用简单,会员可以免费查看联系方式。