收藏自翱翔.Net Blog 的ASP.Net生成静态HTML页!

using System;
using System.IO;
using System.Web;
using System.Text;
namespace TestWeb
{
 
/// <summary>
 
/// StaticHtml 的摘要描述。
 
/// </summary>

 public class StaticHtml
 
{
  
public StaticHtml()
  
{
   
//
   
// TODO: 在此加入建構函式的程式碼
   
//
  }


  
public static bool WriteFile(string strText,string strContent,string strAuthor)
  
{
   
string path=HttpContext.Current.Server.MapPath(@"/TestWeb/news/");
   Encoding code
=Encoding.GetEncoding("big5");
   
string temp= HttpContext.Current.Server.MapPath(@"/TestWeb/news/text.html");
   StreamReader sr
=null;
   StreamWriter st
=null;
   
string str="";
   
try
   
{
    sr
=new StreamReader(temp,code);
    str
=sr.ReadToEnd();
   }

   
catch(Exception ex)
   
{
    HttpContext.Current.Response.Write(ex.Message);
    HttpContext.Current.Response.End();
    sr.Close();
   }

   
string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
   str
=str.Replace("ShowArticle",strText);
   str
=str.Replace("biaoti",strText);
   str
=str.Replace("content",strContent);
   str
=str.Replace("author",strAuthor);
   
try
   
{
    st
= new StreamWriter(path+htmlfilename,false,code);
    st.Write(str);
    st.Flush();

   }

   
catch(Exception ex)
   
{
    HttpContext.Current.Response.Write(ex.Message);
    HttpContext.Current.Response.End();
   }

   
finally
   
{
    st.Close();
   }

   
return true;
  }

 }

}



实现代码为:
if("text"),"Context"),"author")))
    
{
     Response.Write(
"添加成功");
    }

    
else
    
{
     Response.Write(
"生成HTML出错!");
    }

模板
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>ShowArticle</title>
  
 <body>

 biaoti
 <br>
 content<br>
 author
 </body>
</HTML>

posted @ 2005-01-14 14:40  skylai  阅读(192)  评论(0)    收藏  举报