aspx文件生产静态html文件
从网上看到一个aspx转换成html的类,如下
public class AspxToHtml
{
/// <summary>
/// Aspx文件url
/// </summary>
public string strUrl;
/// <summary>
/// 生成html文件的保存路径
/// </summary>
public string strSavePath;
/// <summary>
/// 生成html文件的文件名
/// </summary>
public string strSaveFile;
public AspxToHtml()
{
}
/// <summary>
/// 将strUrl放到strSavePath目录下,保存为strSaveFile
/// </summary>
/// <returns>是否成功</returns>
public bool ExecAspxToHtml()
{
try
{
StringWriter strHTML = new StringWriter();
System.Web.UI.Page myPage = new Page(); //System.Web.UI.Page中有个Server对象,我们要利用一下它
myPage.Server.Execute(strUrl, strHTML); //将asp_net.aspx将在客户段显示的html内容读到了strHTML中
StreamWriter sw = new StreamWriter(strSavePath + strSaveFile, false, System.Text.Encoding.GetEncoding("GB2312"));
//新建一个文件Test.htm,文件格式为GB2312
sw.Write(strHTML.ToString()); //将strHTML中的字符写到Test.htm中
strHTML.Close(); //关闭StringWriter
sw.Close(); //关闭StreamWriter
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 将Url放到Path目录下,保存为FileName
/// </summary>
/// <param name="Url">aspx页面url</param>
/// <param name="Path">生成html文件的保存路径</param>
/// <param name="FileName">生成html文件的文件名</param>
/// <returns></returns>
public bool ExecAspxToHtml(string Url, string Path, string FileName)
{
try
{
StringWriter strHTML = new StringWriter();
System.Web.UI.Page myPage = new Page(); //System.Web.UI.Page中有个Server对象,我们要利用一下它
myPage.Server.Execute(Url, strHTML); //将asp_net.aspx将在客户段显示的html内容读到了strHTML中
StreamWriter sw = new StreamWriter(Path + FileName, false, System.Text.Encoding.GetEncoding("GB2312"));
//新建一个文件Test.htm,文件格式为GB2312
sw.Write(strHTML.ToString()); //将strHTML中的字符写到Test.htm中
strHTML.Close(); //关闭StringWriter
sw.Close(); //关闭StreamWriter
return true;
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return false;
}
}
}
新建两个aspx页面:AspxToHtml.aspx和Test.aspx,
AspxToHtml.aspx.cs内容如下
AspxToHtml aspx = new AspxToHtml();
protected void Page_Load(object sender, EventArgs e)
{
aspx.ExecAspxToHtml(@"Test.aspx", @"C:\", "test.html");
}
这样每当页面加载的时候就把Test.aspx页面生产为Test.html,可能好多网站的新闻也是这样生产的吧。
public class AspxToHtml
{
/// <summary>
/// Aspx文件url
/// </summary>
public string strUrl;
/// <summary>
/// 生成html文件的保存路径
/// </summary>
public string strSavePath;
/// <summary>
/// 生成html文件的文件名
/// </summary>
public string strSaveFile;
public AspxToHtml()
{
}
/// <summary>
/// 将strUrl放到strSavePath目录下,保存为strSaveFile
/// </summary>
/// <returns>是否成功</returns>
public bool ExecAspxToHtml()
{
try
{
StringWriter strHTML = new StringWriter();
System.Web.UI.Page myPage = new Page(); //System.Web.UI.Page中有个Server对象,我们要利用一下它
myPage.Server.Execute(strUrl, strHTML); //将asp_net.aspx将在客户段显示的html内容读到了strHTML中
StreamWriter sw = new StreamWriter(strSavePath + strSaveFile, false, System.Text.Encoding.GetEncoding("GB2312"));
//新建一个文件Test.htm,文件格式为GB2312
sw.Write(strHTML.ToString()); //将strHTML中的字符写到Test.htm中
strHTML.Close(); //关闭StringWriter
sw.Close(); //关闭StreamWriter
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 将Url放到Path目录下,保存为FileName
/// </summary>
/// <param name="Url">aspx页面url</param>
/// <param name="Path">生成html文件的保存路径</param>
/// <param name="FileName">生成html文件的文件名</param>
/// <returns></returns>
public bool ExecAspxToHtml(string Url, string Path, string FileName)
{
try
{
StringWriter strHTML = new StringWriter();
System.Web.UI.Page myPage = new Page(); //System.Web.UI.Page中有个Server对象,我们要利用一下它
myPage.Server.Execute(Url, strHTML); //将asp_net.aspx将在客户段显示的html内容读到了strHTML中
StreamWriter sw = new StreamWriter(Path + FileName, false, System.Text.Encoding.GetEncoding("GB2312"));
//新建一个文件Test.htm,文件格式为GB2312
sw.Write(strHTML.ToString()); //将strHTML中的字符写到Test.htm中
strHTML.Close(); //关闭StringWriter
sw.Close(); //关闭StreamWriter
return true;
}
catch (Exception ex)
{
System.Console.WriteLine(ex.Message);
return false;
}
}
}
新建两个aspx页面:AspxToHtml.aspx和Test.aspx,
AspxToHtml.aspx.cs内容如下
AspxToHtml aspx = new AspxToHtml();
protected void Page_Load(object sender, EventArgs e)
{
aspx.ExecAspxToHtml(@"Test.aspx", @"C:\", "test.html");
}
这样每当页面加载的时候就把Test.aspx页面生产为Test.html,可能好多网站的新闻也是这样生产的吧。