C#动态生成html页

Html生成模块:WriteHtml.cs

复制代码
 1 using System.Collections.Generic;
 2 using System.IO;
 3 using System.Text;
 4 
 5 namespace System
 6 {
 7     /// <summary>
 8     /// Html
 9     /// </summary>
10     public class Html
11     {
12         /// <summary>
13         /// 生成Html
14         /// </summary>
15         /// <param name="template">模版文件</param>
16         /// <param name="path">生成的文件目录</param>
17         /// <param name="htmlname">生成的文件名</param>
18         /// <param name="dic">字典</param>
19         /// <param name="message">异常消息</param>
20         /// <returns></returns>
21         public bool Create(string template, string path, string htmlname, Dictionary<string, string> dic, ref string message)
22         {
23             bool result = false;
24             string templatepath = System.Web.HttpContext.Current.Server.MapPath(template);
25             string htmlpath = System.Web.HttpContext.Current.Server.MapPath(path);
26             string htmlnamepath = Path.Combine(htmlpath, htmlname);
27             Encoding encode = Encoding.UTF8;
28             StringBuilder html = new StringBuilder();
29 
30             try
31             {
32                 //读取模版
33                 html.Append(File.ReadAllText(templatepath, encode));
34             }
35             catch (FileNotFoundException ex)
36             {
37                 message = ex.Message;
38                 return false;
39             }
40 
41             foreach (KeyValuePair<string,string> d in dic)
42             {
43                 //替换数据
44                 html.Replace(
45                     string.Format("${0}$", d.Key),
46                     d.Value);
47             }
48 
49             try
50             {
51                 //写入html文件
52                 if (!Directory.Exists(htmlpath))
53                     Directory.CreateDirectory(htmlpath);
54                 File.WriteAllText(htmlnamepath, html.ToString(), encode);
55                 result = true;
56             }
57             catch (IOException ex)
58             {
59                 message = ex.Message;
60                 return false;
61             }
62 
63             return result;
64         }
65     }
66 }
复制代码

模版文件:/Template/a.html

复制代码
 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 5     <title>$title$</title>
 6 </head>
 7 <body>
 8     $content$<br/>
 9     $author$
10 </body>
11 </html>
复制代码

调用网页:test.ashx

复制代码
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Web;
 4 
 5 namespace Wycz
 6 {
 7     /// <summary>
 8     /// test 的摘要说明
 9     /// </summary>
10     public class test : IHttpHandler
11     {
12 
13         public void ProcessRequest(HttpContext context)
14         {
15             //context.Response.ContentType = "text/plain";
16             //context.Response.Write("Hello World");
17             string template = "/Template/a.html";
18             string path = "/test/";
19             string htmlname = "a.html";
20             Dictionary<string, string> dic = new Dictionary<string, string>();
21             Html h = new Html();
22             string message = string.Empty;
23 
24             dic.Add("title", "动态生成html");
25             dic.Add("content", "测试内容");
26             dic.Add("author", "P.R");
27 
28             if (!h.Create(template, path, htmlname, dic, ref message))
29             {
30                 context.Response.Write("出错啦:<br/>");
31                 context.Response.Write(message);
32                 context.Response.End();
33             }
34 
35             context.Response.Redirect(path + htmlname);
36         }
37 
38         public bool IsReusable
39         {
40             get
41             {
42                 return false;
43             }
44         }
45     }
46 }
复制代码

效果图:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值