ASP.NET页面生成HTML
网上看到很多人问NET生成HTML的相关技术,我以前也曾为这个问题伤脑筋很久,网上的很多是模版替换
字符的方法来实现,在实际的应用中,那样的方法根本就没有用。其实生成HTML的原理就是读取已经存在
的网站的HTML代码,再保存为其他文件,命名为****.htm或****.html。我自己写了一个类,使用很简单
,比较容易懂,也希望能给大家一点启示。
类名为schtml.cs
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Web;
5
using System.Web.Security;
6
using System.Web.UI;
7
using System.Web.UI.HtmlControls;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.Hosting;
11
using System.IO;
12
using System.Text;
13
using System.Net;
14
15
/// <summary>
16
///schtml 的摘要说明
17
/// </summary>
18
public class schtml
19
{
20
public schtml()
21
{
22
//
23
//TODO: 在此处添加构造函数逻辑
24
//
25
}
26
27
28
//生成html
29
public void creathtml(string url,string path)
30
{
31
Encoding code = Encoding.GetEncoding("utf-8");
32
StreamReader sr = null;
33
StreamWriter sw = null;
34
string str = null;
35
36
//读取远程路径
37
WebRequest temp = WebRequest.Create("http://" + HttpContext.Current.Request.Url.Host.ToString() + url);
38
WebResponse myTemp = temp.GetResponse();
39
sr = new StreamReader(myTemp.GetResponseStream(), code);
40
//读取
41
try
42
{
43
sr = new StreamReader(myTemp.GetResponseStream(), code);
44
str = sr.ReadToEnd();
45
46
}
47
catch (Exception ex)
48
{
49
throw ex;
50
}
51
finally
52
{
53
sr.Close();
54
}
55
56
//写入
57
try
58
{
59
sw = new StreamWriter(path, false, code);
60
sw.Write(str);
61
sw.Flush();
62
63
}
64
catch (Exception ex)
65
{
66
throw ex;
67
}
68
finally
69
{
70
sw.Close();
71
72
}
73
74
}
75
}
76
using System; 2
using System.Data; 3
using System.Configuration; 4
using System.Web; 5
using System.Web.Security; 6
using System.Web.UI; 7
using System.Web.UI.HtmlControls; 8
using System.Web.UI.WebControls; 9
using System.Web.UI.WebControls.WebParts; 10
using System.Web.Hosting; 11
using System.IO; 12
using System.Text; 13
using System.Net; 14

15
/// <summary> 16
///schtml 的摘要说明 17
/// </summary> 18
public class schtml 19
{ 20
public schtml() 21
{ 22
// 23
//TODO: 在此处添加构造函数逻辑 24
// 25
} 26

27

28
//生成html 29
public void creathtml(string url,string path) 30
{ 31
Encoding code = Encoding.GetEncoding("utf-8"); 32
StreamReader sr = null; 33
StreamWriter sw = null; 34
string str = null; 35
36
//读取远程路径 37
WebRequest temp = WebRequest.Create("http://" + HttpContext.Current.Request.Url.Host.ToString() + url); 38
WebResponse myTemp = temp.GetResponse(); 39
sr = new StreamReader(myTemp.GetResponseStream(), code); 40
//读取 41
try 42
{ 43
sr = new StreamReader(myTemp.GetResponseStream(), code); 44
str = sr.ReadToEnd(); 45

46
} 47
catch (Exception ex) 48
{ 49
throw ex; 50
} 51
finally 52
{ 53
sr.Close(); 54
} 55

56
//写入 57
try 58
{ 59
sw = new StreamWriter(path, false, code); 60
sw.Write(str); 61
sw.Flush(); 62

63
} 64
catch (Exception ex) 65
{ 66
throw ex; 67
} 68
finally 69
{ 70
sw.Close(); 71

72
} 73

74
} 75
}76

使用方法也很简单
1
schtml html1 = new schtml();
2
html1.creathtml("/product/default.aspx", Server.MapPath("http://www.cnblogs.com/product/") + "index.htm");
schtml html1 = new schtml(); 2
html1.creathtml("/product/default.aspx", Server.MapPath("http://www.cnblogs.com/product/") + "index.htm");
html1.creathtml的两个参数说明一下:
前面一个是页面路径/product/default.aspx
后面一个Server.MapPath("http://www.cnblogs.com/product/") + "index.htm",是指要把生成的文件保存在哪个文件夹,此处是保存在product目录下,文件名为index.html
可以相互交流相互学习



浙公网安备 33010602011771号