(c#)asp.net生成静态页面时如何写模板 模板很好写<!--模块文件(template.htm)--><html><head><title>$TITLE$</title></head><body>$CONTENT$</body></html> 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Web;using System.IO;using System.Text;/// <summary>/// Summary description for CreatHTML/// </summary>public class CreatHTML{ public CreatHTML() { // // TODO: Add constructor logic here // } /// <summary> /// 生成模板静态页方法 /// </summary> /// <param name="fileName">HTML文件名称</param> /// <param name="templateParams">参数数组,参数为: 标签名称|替换内容</param> /// <returns></returns> public static bool WriteHtmlFile(string fileName,params string[] templateParams) { bool succeed = false; string[] tempArray = null; if (string.IsNullOrEmpty(fileName)) { return (succeed); } if (templateParams.Length < 1) { return (succeed); } string sTime = DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo); //string path = HttpContext.Current.Server.MapPath("/" + sTime + "/"); string path = @"E:\work\web\CSharp\WebTry\" + sTime + "\\"; string addtime = DateTime.Now.ToString(); Encoding code = Encoding.GetEncoding("gb2312"); // 读取模板文件 //string temp = HttpContext.Current.Server.MapPath("/template/template.html"); string temp = @"E:\work\web\CSharp\WebTry\template.htm"; StreamReader sr = null; StreamWriter sw = null; string str = ""; try { sr = new StreamReader(temp, code); str = sr.ReadToEnd(); // 读取文件 } catch (Exception exp) { HttpContext.Current.Response.Write(exp.Message); HttpContext.Current.Response.End(); sr.Close(); } //html文件名称 string htmlFileName = fileName + ".html"; //取出参数 for (int i = 0; i < templateParams.Length; i++) { tempArray = templateParams[i].Split('|'); if (tempArray.Length != 2) { return (succeed); } // 替换内容 str = str.Replace(tempArray[0], tempArray[1]); } // 写文件 if (!System.IO.Directory.Exists(path))//不存在路径 { try { System.IO.Directory.CreateDirectory(path); sw = new StreamWriter(path + htmlFileName, false, code); sw.Write(str); sw.Flush(); succeed = true; } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); } finally { sw.Close(); } } else//存在路径 { try { sw = new StreamWriter(path + htmlFileName, false, code); sw.Write(str); sw.Flush(); succeed = true; } catch (Exception ex) { HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); } finally { sw.Close(); sr.Close(); } } return (succeed); }} 你这是动态页生成静态页,我要的那种是,像http://www.iwms.net/这样的,由模板页拼成一块块的显示出来,你可以看一下这个网站 求助,WPF4程序,MessageBox样式问题 Word格式问题 CheckedListBox 将处于选中状态的都置为高亮显示,怎样实现 谢谢 为一个基础问题 多线程访问窗体控件出错 各位老师,请来看看这个疑难问题! 窗体最大化的问题? 高分求助。急盼大家回音。 如何列出oracle中某个数据库下用户建的表及每个表的字段? 如何设置openFileDialog的大小,不是显示后用鼠标来控制? 各位大哥,小妹求助C#打印锯齿问题。 新手
using System.Web;
using System.IO;
using System.Text;/// <summary>
/// Summary description for CreatHTML
/// </summary>
public class CreatHTML
{
public CreatHTML()
{
//
// TODO: Add constructor logic here
//
}
/// <summary>
/// 生成模板静态页方法
/// </summary>
/// <param name="fileName">HTML文件名称</param>
/// <param name="templateParams">参数数组,参数为: 标签名称|替换内容</param>
/// <returns></returns>
public static bool WriteHtmlFile(string fileName,params string[] templateParams)
{
bool succeed = false;
string[] tempArray = null;
if (string.IsNullOrEmpty(fileName))
{
return (succeed);
}
if (templateParams.Length < 1)
{
return (succeed);
}
string sTime = DateTime.Now.ToString("yyyyMMdd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
//string path = HttpContext.Current.Server.MapPath("/" + sTime + "/");
string path = @"E:\work\web\CSharp\WebTry\" + sTime + "\\";
string addtime = DateTime.Now.ToString();
Encoding code = Encoding.GetEncoding("gb2312");
// 读取模板文件
//string temp = HttpContext.Current.Server.MapPath("/template/template.html");
string temp = @"E:\work\web\CSharp\WebTry\template.htm";
StreamReader sr = null;
StreamWriter sw = null;
string str = ""; try
{
sr = new StreamReader(temp, code);
str = sr.ReadToEnd(); // 读取文件
}
catch (Exception exp)
{
HttpContext.Current.Response.Write(exp.Message);
HttpContext.Current.Response.End();
sr.Close();
} //html文件名称
string htmlFileName = fileName + ".html";
//取出参数
for (int i = 0; i < templateParams.Length; i++)
{
tempArray = templateParams[i].Split('|');
if (tempArray.Length != 2)
{
return (succeed);
}
// 替换内容
str = str.Replace(tempArray[0], tempArray[1]);
} // 写文件
if (!System.IO.Directory.Exists(path))//不存在路径
{
try
{
System.IO.Directory.CreateDirectory(path);
sw = new StreamWriter(path + htmlFileName, false, code);
sw.Write(str);
sw.Flush();
succeed = true;
}
catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
}
}
else//存在路径
{
try
{
sw = new StreamWriter(path + htmlFileName, false, code);
sw.Write(str);
sw.Flush();
succeed = true;
} catch (Exception ex)
{
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.End();
}
finally
{
sw.Close();
sr.Close();
}
}
return (succeed);
}
}