您的位置:首页 > Web前端 > HTML

C#动态生成html页

2013-07-15 10:11 218 查看
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 }


效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: