有没有C#.net生成html代码的类啊,我想生成一个全静态的网站,除了后台

解决方案 »

  1.   


    for(int N=1;N<=100;N++)
    {
    MessageBox.Show("自顶“+N+”下")
    }
      

  2.   

    看看是否有用.
    http://www.th7.cn/Article/bc/nt/200704/20646_2.html
      

  3.   

    用System.IO往文件里写html代码就行了
      

  4.   

    public static bool WriteFile(string strText) 
        { 
            string path = HttpContext.Current.Server.MapPath("Html/"); 
            System.Text.Encoding code = System.Text.Encoding.GetEncoding("gb2312"); 
            string temp = HttpContext.Current.Server.MapPath("Html/A.html"); 
            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(); 
            } 
            string htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html"; 
            //通过正则获取标签内容实现内容替换 
            try 
            { 
                sw = new StreamWriter(path + htmlfilename, false, code); 
                sw.Write(str); 
                sw.Flush(); 
            } 
            catch (Exception ex) 
            { 
                HttpContext.Current.Response.Write(ex.Message); 
                HttpContext.Current.Response.End(); 
            } 
            finally 
            { 
                sw.Close(); 
            } 
            return true; 
        } 
      

  5.   


    这个我知道,只是如果是从数据库里读取数据再生成HTML的话要遍历数据库,那要怎样遍历呢,最好是C#代码
      

  6.   

    /// <summary>
        /// 创建html页面
        /// </summary>
        protected void Create(string createHtmlTemplatePage, string htmlPage)   //createHtmlTemplatePage:以哪个页面为模板生成html,htmlPage:生成的静态页的名字
        {
            string sUrl = "", sHtmlContent = "", sHost = "", sPath = "", sCurPage = "";        sHost = Request.Url.Authority;
            sCurPage = Request.Url.Segments[Request.Url.Segments.Length - 1];
            sPath = Request.Path.Replace(sCurPage, createHtmlTemplatePage);
            sUrl = "http://" + sHost + sPath;        System.Net.WebRequest wReq = System.Net.WebRequest.Create(sUrl);
            System.Net.WebResponse wResp = wReq.GetResponse();
            System.IO.Stream respStream = wResp.GetResponseStream();
            System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8"));        sHtmlContent = reader.ReadToEnd();
            respStream.Dispose();
            reader.Dispose();
            if (File.Exists(Server.MapPath(htmlPage)))
            {
                File.Delete(htmlPage);
            }
            //写入文件
            StreamWriter sw;
            sw = new StreamWriter(Server.MapPath(htmlPage), false, System.Text.Encoding.GetEncoding("utf-8"));
            sw.Write(sHtmlContent);
            sw.Dispose();
        }
    封装之后直接调用
      

  7.   

    用自己html模板,然后读取,生成字符串,用流输出就行了
      

  8.   

    我都是用xx.replace("$html标记$","替换的内容")做的   不知道还有没有更好的实现?
      

  9.   

    重写Render方法,还需要类吗?
      

  10.   

    http://blog.csdn.net/durongjian/archive/2010/01/26/5257208.aspx
      

  11.   

    这个回复不管用 是用来获取网页源代码的代码提醒一下(利用WebClient和WebRequest类获得网页源代码)
    呵呵
      

  12.   

    ASP.Net生成静态HTML页
    在Asp中实现的生成静态页用到的FileSystemObject对象!
    在.Net中涉及此类操作的是System.IO 
    以下是程序代码 注:此代码非原创!参考别人代码
    //生成HTML页
      public static bool WriteFile(string strText,string strContent,string strAuthor) 
      {
       string path = HttpContext.Current.Server.MapPath("/news/");
       Encoding code = Encoding.GetEncoding("gb2312");
       // 读取模板文件
       string temp = HttpContext.Current.Server.MapPath("/news/text.html");
       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();
       }
      
       
       string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
       // 替换内容
       // 这时,模板文件已经读入到名称为str的变量中了
       str =str.Replace("ShowArticle",strText); //模板页中的ShowArticle
       str = str.Replace("biaoti",strText);
       str = str.Replace("content",strContent);
       str = str.Replace("author",strAuthor);
       // 写文件
       try
       {
        sw = new StreamWriter(path + htmlfilename , false, code);
        sw.Write(str);
        sw.Flush();
       }
       catch(Exception ex)
       {
        HttpContext.Current.Response.Write(ex.Message);
        HttpContext.Current.Response.End();
       }
       finally
       {
        sw.Close();
       }
       return true;此函数放在Conn.CS基类中了
    在添加新闻的代码中引用 注:工程名为Hover
     
        if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
        {
         Response.Write("添加成功");
        }
        else
        {
         Response.Write("生成HTML出错!");
        }
    -------------------------------------------------------------------------
    模板页Text.html代码
    -------------------------------------------------------------------------
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
     <HEAD>
      <title>ShowArticle</title>
      
     <body> biaoti
     <br>
     content<br>
     author
     </body>
    </HTML>
      

  13.   

       protected void html()   ////生成新闻静态
        {
            //String Path = Server.MapPath("");        String txtPath = Server.MapPath("");        String ConnStr = ConfigurationManager.ConnectionStrings["CSDN"].ToString();        String StrSql = @"Select * From Content";        SqlDataAdapter DA = new SqlDataAdapter(StrSql, ConnStr);        DataSet DS = new DataSet();        DA.Fill(DS,"Tables");        StreamReader sr = null;
            StreamWriter sw = null;
            Encoding code = Encoding.GetEncoding("gb2312");        try
            {
                String str = "";
                sr = new StreamReader(txtPath + @"\temp_xinwen.html", code);            str = sr.ReadToEnd();            foreach (DataRow dr in DS.Tables[0].Rows)
                {
                    String tempStr = str;                tempStr = tempStr.Replace("{$tape}", dr["title"].ToString());
                    tempStr = tempStr.Replace("{$title}", dr["title"].ToString());
                    tempStr = tempStr.Replace("{$content}",dr["content"].ToString());
                    tempStr = tempStr.Replace("{$1}", dr["title"].ToString());
                    tempStr = tempStr.Replace("{$url}", dr["down"].ToString());
                    try
                    {
                        sw = new StreamWriter(txtPath +@"\html_xinwen" +"\\" + dr["id"].ToString() + ".Html", false, code);
                        sw.Write(tempStr);
                        sw.Flush();                }
                    catch (Exception ex)
                    {
                        if (sw != null)
                        {
                            sw.Close();
                        }
                    }            }            Response.Write("<script>alert('已经生成N个静态新闻页面');</script>");        }
            catch
            {
                if (sr != null)
                    sr.Close();        }}
      

  14.   

    你看一下http://www.csharp360.com/bbs/viewthread.php?tid=84&extra=page%3D1
    可能对你有用