c aspx转html,ASPX文件转HTML

ASPX文件转HTML

经过验证 下面大致5个方法均可以成功转化,但是转化过程中一定要注意原ASPX文件的编码类型,如果是GB2312的 那么不管是在streamReader的时候,还是先将原数据转化成byte流,再转化出HTML字符串的时候一定要注意同原ASPX页面编码类型相同,否则将出现乱码。

using System.Net;

using System.Text;

using System.IO;

public partial class _Default : System.Web.UI.Page

{

public StreamWriter sw;

protected void Page_Load(object sender, EventArgs e)

{

WebClient myWebClient = new WebClient();

myWebClient.Credentials = CredentialCache.DefaultCredentials;

byte[] pagedata = myWebClient.DownloadData("http://home.xmhouse.com/default.aspx");

string myDataBuffer = Encoding.Default.GetString(pagedata);

string path = HttpContext.Current.Server.MapPath(".");

Encoding code = Encoding.GetEncoding("gb2312");

string htmlfilename = "test.html";

try

{

FileStream fs = new FileStream(path+"/"+htmlfilename, FileMode.Create, FileAccess.Write);

StreamWriter sw = new StreamWriter(fs, Encoding.Default);

sw.WriteLine(myDataBuffer);

sw.Close();

fs.Close();

Response.Write("生成成功了!ok");

}

catch (Exception ex)

{

File.Delete(path + htmlfilename);

HttpContext.Current.Response.Write(ex.Message);

HttpContext.Current.Response.End();

Response.Write("生成失败了!no");

}

finally

{

if (sw != null)

sw.Close();

}

}

}

我们开发的asp.net系统中,有些动态的页面常被频繁,如我们的首页index.aspx它涉及到大量的数据库查询工作,当不断有用户它时,服务器便不断向数据库的查询,实际上做了许多重复的工作

服务器端的myPage.aspx

客户端显示myPage.htm

客户端

针对这种资源的浪费情况,我们现在来设计一个解决方案。我们先将那些一段时间内内容不会有什么改变,但又遭大量的动态页面生成静态的页面存放在服务器上,当客户端发出请求时,就让他们直接我们生成的静态页面,过程如下图。

客户端显示myPage.htm

客户端

Execute

服务器端的myPage.aspx

服务器端的myPage.htm

现在我们需要一个后台程序来完成这些事情。

我们可将此做成一个类classAspxToHtml ,其代码

using System;

using System.IO;

using System.Web.UI;

namespace LeoLu

{

/// summary

/// AspxToHtml 的摘要说明。

/// /summary

public class AspxToHtml

{

/// summary

/// Aspx文件url

/// /summary

public string strUrl;

/// summary

/// 生成html文件的保存路径

/// /summary

public string strSavePath;

/// summary

/// 生成html文件的文件名

/// /summary

public string strSaveFile;

public AspxToHtml()

{

//

// TOD 在此处添加构造函数逻辑

//

}

/// 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,true,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,true,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;

}

}

}

}

转自http://www.cnblogs.com/chengyan/archive/2010/12/21/1912335.html

其他 方法:

------------------------------

using system.net;

using system.io;

First:在服务器上指定aspx网页,生成html静态页

public partial class Default2 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

StreamWriter sw = new StreamWriter(Server.MapPath("静态页1.htm"), false, System.Text.Encoding.GetEncoding("gb2312"));

Server.Execute("Default3.aspx", sw);

sw.Close();

}

}

}

Second:在服务器上执行aspx网页时在page_render事件里将本页面生成html静态页

public partial class Default3 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected override void Render(HtmlTextWriter writer)

{

StringWriter html = new StringWriter();

System.Web.UI.HtmlTextWriter tw = new System.Web.UI.HtmlTextWriter(html);

base.Render(tw);

System.IO.StreamWriter sw;

sw = new System.IO.StreamWriter(Server.MapPath("静态页2.htm"), false, System.Text.Encoding.Default);

sw.Write(html.ToString());

sw.Close();

tw.Close();

Response.Write(html.ToString());

}

}

Third:从指定连接获取源代码生成html静态页

public partial class Default4 : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

string pageurl = "http://www.baidu.com";

WebRequest request = WebRequest.Create(pageurl);

WebResponse response = request.GetResponse();

Stream resstream = response.GetResponseStream();

StreamReader sr = new StreamReader(resstream, System.Text.Encoding.Default);

string contenthtml = sr.ReadToEnd();

resstream.Close();

sr.Close(); //写入文件

System.IO.StreamWriter sw;

sw = new System.IO.StreamWriter(Server.MapPath("静态页生成方法3.htm"), false, System.Text.Encoding.Default);

sw.Write(contenthtml);

sw.Close();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值