c 自动生成html模板,根据html页面模板动态生成html页面(c#类)_c#应用-站长资讯中心...

c4468b3f4df77e96b0a416fa2a870fba.png

一直以为动态生成静态页面不好做,昨天在网上找了下,我晕,其实很简单,思路大概是这样的,

1:建立一个html页面模板,在这个页面中把您想要动态显示的地方用特别的字符串表示(如$htmlstrstr$);

2:在程式中用将这个html页面读到一个字符串变量如str;

3:用字符串的resplace方法将在第一步中特别字符替换成您想要的内容;

4保存;

OK,so easy,今天就用C#写了一个这样的类,用来处理动态生成html页面的,自认为还写的完整,刚接触.NET不久,望指教,完整代码和示例在此下载:下载http://www.cnblogs.com/Files/solucky/aspxTohtml.rar

转贴请注明出处,谢谢!

注:此类中的代码不全是原创,部份代码参照网友的代码!

以下是转换类的代码

代码

1using System;

2using System.Text;

3using System.Web;

4using System.Configuration;

5using System.IO;

6namespace solucky

7{

8 /**

9 /// AspxToHtml 的摘要说明。

10 /// 注:使用此类,您能够在web.config文档对模板类进行配置.如下

11 /**//*

12

13

14

15 */

16 /**

17 public class AspxToHtml

18 {

19 /**

20 /// 模板文档中要替代的参数个数

21 ///

22 private int _templateParamCount=0;

23 /**

24 /// 模板文档所在的路径

25 ///

26 private string _templateFilePath =ConfigurationSettings.AppSettings["templateFilePath"];

27 /**

28 /// 转换后的html文档所存放的路径

29 ///

30 private string _htmlFilePath =ConfigurationSettings.AppSettings["htmlFilePath"];

31

32 /**

33 /// 模板页页面编码

34 ///

35 private Encoding _templateHtmlCode =Encoding.GetEncoding("gb2312");

36

37 /**

38 /// 转换后的文档编码

39 ///

40 private Encoding _code = Encoding.GetEncoding("gb2312");

41

42 /**

43 /// 转换后的html文档名

44 ///

45 private string _convertedFilename="";

46 /**

47 /// 模板文档中的参数

48 ///

49 private string[] _templateFileparameter ;

50

51 /**

52 /// aspx文档中的要代替HTML文档中的参数实际值

53 ///

54 private string[] _aspxFileparameter;

55

56 private string _errlogPath = ConfigurationSettings.AppSettings["ErrLogPath"];

57

58 属性#region 属性

59

60 /**

61 /// 模板文档中要替代的参数个数

62 ///

63 public int TemplateParamCount

64 {

65 get

66 {

67 return this._templateParamCount;

68 }

69 set//分配参数个数时,同时为模板文档中的参数和aspx文档中的要代替HTML文档中的参数实际值这两个分配实际数组

70 {

71 if (value < 0)

72 throw new ArgumentException();

73

74 if(value>0)

75 {

76 this._templateParamCount=value;

77 //模板文档中的参数

78 _templateFileparameter = new string[value];

79 //aspx文档中的要代替HTML文档中的参数实际值

80 _aspxFileparameter = new string[value];

81 }

82 else

83 this._templateParamCount=0;

84 }

85 }

86

87 /**

88 /// 模板文档所在的路径

89 ///

90 ///

91 public string TemplateFilePath

92 {

93 get{ return this._templateFilePath;}

94 set{ this._templateFilePath=value;}

95 }

96 /**

97 /// 转换后的html文档所存放的路径

98 ///

99 public string HtmlFilePath

100 {

101 get{ return this._htmlFilePath;}

102 set{ this._htmlFilePath=value;}

103 }

104

105 /**

106 /// html模板文档编码

107 ///

108 public Encoding TemplateHtmlCode

109 {

110 get{ return this._templateHtmlCode;}

111 set{ this._templateHtmlCode=Encoding.GetEncoding(value.ToString());}

112 }

113 /**

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值