![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
ASP.NET.如何生成HTML静态页面?。
5个回答
2013-08-08
展开全部
1.定义(template.htm)html模板页面
<html>
<head>
<title>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body >
<table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#e0e1e0" style="border:1px solid #010000">
<tr>
<td width="100%" valign="middle" align="left">
<span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span>
</td>
</tr>
</table>
</body>
</html>
2.asp.net代码:
//读html模板页面到stringbuilder对象里
string[] format=new string[4];//定义和htmlyem标记数目一致的数组
StringBuilder htmltext=new StringBuilder();
try
{
using (StreamReader sr = new StreamReader("存放模板页面的路径和页面名"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
catch
{
Response.Write("<Script>alert('读取文件错误')</Script>");
}
//给标记数组赋值
format[0]="background=\"bg.jpg\"";//背景图片
format[1]= "#990099";//字体颜色
format[2]="150px";//字体大小
format[3]= "<marquee>生成的模板html页面</marquee>";//文字说明
//替换htm里的标记为你想加的内容
for(int i=0;i<4;i++)
{
htmltext.Replace("$htmlformat["+i+"]",format[i]);
}
//生成htm文件
try
{
using(StreamWriter sw=new StreamWriter("存放路径和页面名",false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
}
catch
{
Response.Write ("The file could not be wirte:");
}
<html>
<head>
<title>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body >
<table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#e0e1e0" style="border:1px solid #010000">
<tr>
<td width="100%" valign="middle" align="left">
<span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span>
</td>
</tr>
</table>
</body>
</html>
2.asp.net代码:
//读html模板页面到stringbuilder对象里
string[] format=new string[4];//定义和htmlyem标记数目一致的数组
StringBuilder htmltext=new StringBuilder();
try
{
using (StreamReader sr = new StreamReader("存放模板页面的路径和页面名"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
htmltext.Append(line);
}
sr.Close();
}
catch
{
Response.Write("<Script>alert('读取文件错误')</Script>");
}
//给标记数组赋值
format[0]="background=\"bg.jpg\"";//背景图片
format[1]= "#990099";//字体颜色
format[2]="150px";//字体大小
format[3]= "<marquee>生成的模板html页面</marquee>";//文字说明
//替换htm里的标记为你想加的内容
for(int i=0;i<4;i++)
{
htmltext.Replace("$htmlformat["+i+"]",format[i]);
}
//生成htm文件
try
{
using(StreamWriter sw=new StreamWriter("存放路径和页面名",false,System.Text.Encoding.GetEncoding("GB2312")))
{
sw.WriteLine(htmltext);
sw.Flush();
sw.Close();
}
}
catch
{
Response.Write ("The file could not be wirte:");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-08
展开全部
public class WriteFile
{
public WriteFile()
{
}
public static bool createHtml(string[] strnewsHtml,string[] stroldHtml,string strModeFilePath,string strPath)
{
bool flag = false;
StreamReader sr = null;
StreamWriter sw = null;
string filepath = HttpContext.Current.Server.MapPath(strModeFilePath);
Encoding code = Encoding.GetEncoding("gb2312");
string s = string.Empty;
try
{
sr = new StreamReader(filepath,code);
s = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
try
{
for (int i = 0; i < strnewsHtml.Length; i++)
{
s = s.Replace(stroldHtml[i], strnewsHtml[i]);
}
sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code);
sw.Write(s);
flag = true;
}
catch (Exception ex)
{
flag = false;
throw ex;
}
finally
{
sw.Flush();
sw.Close();
}
return flag;
}
public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)
{
bool Flage = false;
StreamReader ReaderFile = null;
StreamWriter WrirteFile = null;
string FilePath = HttpContext.Current.Server.MapPath(strHtml);
Encoding Code = Encoding.GetEncoding("gb2312");
string strFile = string.Empty;
try
{
ReaderFile = new StreamReader(FilePath, Code);
strFile = ReaderFile.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
ReaderFile.Close();
}
try
{
int intLengTh = strNewsHtml.Length;
for (int i = 0; i < intLengTh; i++)
{
int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
int intEnd = strFile.IndexOf(strEndHtml[i]);
string strOldHtml = strFile.Substring(intStart, intEnd - intStart);
strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);
}
WrirteFile = new StreamWriter(FilePath, false, Code);
WrirteFile.Write(strFile);
Flage = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
WrirteFile.Flush();
WrirteFile.Close();
}
return Flage;
}
}
{
public WriteFile()
{
}
public static bool createHtml(string[] strnewsHtml,string[] stroldHtml,string strModeFilePath,string strPath)
{
bool flag = false;
StreamReader sr = null;
StreamWriter sw = null;
string filepath = HttpContext.Current.Server.MapPath(strModeFilePath);
Encoding code = Encoding.GetEncoding("gb2312");
string s = string.Empty;
try
{
sr = new StreamReader(filepath,code);
s = sr.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
sr.Close();
}
try
{
for (int i = 0; i < strnewsHtml.Length; i++)
{
s = s.Replace(stroldHtml[i], strnewsHtml[i]);
}
sw = new StreamWriter(HttpContext.Current.Server.MapPath(strPath), false, code);
sw.Write(s);
flag = true;
}
catch (Exception ex)
{
flag = false;
throw ex;
}
finally
{
sw.Flush();
sw.Close();
}
return flag;
}
public static bool UpdateHtmlPage(string[] strNewsHtml, string[] strStartHtml, string[] strEndHtml, string strHtml)
{
bool Flage = false;
StreamReader ReaderFile = null;
StreamWriter WrirteFile = null;
string FilePath = HttpContext.Current.Server.MapPath(strHtml);
Encoding Code = Encoding.GetEncoding("gb2312");
string strFile = string.Empty;
try
{
ReaderFile = new StreamReader(FilePath, Code);
strFile = ReaderFile.ReadToEnd();
}
catch (Exception ex)
{
throw ex;
}
finally
{
ReaderFile.Close();
}
try
{
int intLengTh = strNewsHtml.Length;
for (int i = 0; i < intLengTh; i++)
{
int intStart = strFile.IndexOf(strStartHtml[i]) + strStartHtml[i].Length;
int intEnd = strFile.IndexOf(strEndHtml[i]);
string strOldHtml = strFile.Substring(intStart, intEnd - intStart);
strFile = strFile.Replace(strOldHtml, strNewsHtml[i]);
}
WrirteFile = new StreamWriter(FilePath, false, Code);
WrirteFile.Write(strFile);
Flage = true;
}
catch (Exception ex)
{
throw ex;
}
finally
{
WrirteFile.Flush();
WrirteFile.Close();
}
return Flage;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-08
展开全部
你可以直接用DREAMWEAVER做啊,不用ASP.NET来做啊!~或者直接用代码进行,同样也可以做出静态网页
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-08-08
展开全部
string NewsTitle = this.TextBox1.Text;
string NewsKindName = this.DropDownList1.SelectedItem.Text;
string NewsBody = this.WebEditor1.Text;
DateTime PubTime = DateTime.Now;
string UserName = Session["UserName"].ToString();
Response.Write(NewsKindName);
string[] strNewsHtml = new string[] { NewsTitle, NewsKindName, NewsBody, PubTime.ToString(), UserName };
string[] strOldHtml = new string[] { "@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName" };
string strFileName = DateTime.Now.ToString("ddhhmmss") + ".html";
string strFilePath = string.Format("NewsHtml/{0}", strFileName);
try
{
if (WriteFile.createHtml(strNewsHtml, strOldHtml, "mode.htm", strFilePath))
{
this.Label1.Text = "生成成功!";
}
else
{
this.Label1.Text = "生成失败!";
}
}
catch
{
this.Label1.Text = "生成失败!";
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string[] strNewsHtml=new string[]{"到此一游!"};
string[] strStartHtml=new string[]{""};
string[] strEndHtml=new string[]{""};
if (WriteFile.UpdateHtmlPage(strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/02011139.html"))
{
this.Label1.Text="生成首页成功!";
}
else
{
this.Label1.Text="生成首页失败!";
}
}
string NewsKindName = this.DropDownList1.SelectedItem.Text;
string NewsBody = this.WebEditor1.Text;
DateTime PubTime = DateTime.Now;
string UserName = Session["UserName"].ToString();
Response.Write(NewsKindName);
string[] strNewsHtml = new string[] { NewsTitle, NewsKindName, NewsBody, PubTime.ToString(), UserName };
string[] strOldHtml = new string[] { "@Title", "@NewsKInd", "@NewsBody", "@PubTime", "@UserName" };
string strFileName = DateTime.Now.ToString("ddhhmmss") + ".html";
string strFilePath = string.Format("NewsHtml/{0}", strFileName);
try
{
if (WriteFile.createHtml(strNewsHtml, strOldHtml, "mode.htm", strFilePath))
{
this.Label1.Text = "生成成功!";
}
else
{
this.Label1.Text = "生成失败!";
}
}
catch
{
this.Label1.Text = "生成失败!";
}
}
protected void Button3_Click(object sender, EventArgs e)
{
string[] strNewsHtml=new string[]{"到此一游!"};
string[] strStartHtml=new string[]{""};
string[] strEndHtml=new string[]{""};
if (WriteFile.UpdateHtmlPage(strNewsHtml, strStartHtml, strEndHtml, "NewsHtml/02011139.html"))
{
this.Label1.Text="生成首页成功!";
}
else
{
this.Label1.Text="生成首页失败!";
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
常用的是根据模板标签生成,读取htm内容,然后再将htm里面的标签替换为你想要的内容,然后再保存为html,看下nongfuit#com,把#换成点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询