Asp.net生成静态页面最简单方法(源码)

简介: public string IssuanceHTML(string NC_Id_S) { string returnValue = ""; string htmlfilename=DateTime.

public string IssuanceHTML(string NC_Id_S)

{

string returnValue = "";

string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";

        SqlConnection conn = null;

        SqlDataReader sdr = null;

        try

        {

            string[] NC_Id_SS = NC_Id_S.Split(','); //批量操作时 Split分离数据

            for (int i = 0; i < NC_Id_SS.Length; i++)  //循环生成

            {conn = Connection.CreateDbConn();

             conn.Open();

             SqlCommand cmd = new SqlCommand();

cmd.Connection = conn;

    cmd.CommandText = "SELECT NC_Name_S, NC_Content_S FROM NewsContent WHERE NC_Id_S = '" + NC_Id_S + "'";

    sdr = cmd.ExecuteReader();

sdr.Read();

string path = HttpContext.Current.Server.MapPath("/HTML/"); //生成文件路径

    Encoding code = Encoding.GetEncoding("gb2312"); //编码规则

    string temp = HttpContext.Current.Server.MapPath("/Templet/C8Article.html"); 模版文件

    StreamReader sr = null;

    StreamWriter sw = null;

    string str = "";

    sr = new StreamReader(temp, code);

    str = sr.ReadToEnd();

    sr.Close();

    str = str.Replace("spagetitle", sdr.GetString(0).ToString());//spagetitle为模版文件中内容

    str = str.Replace("scontent", sdr.GetString(1).ToString());

    sw = new StreamWriter(path + htmlfilename, false, code);

    sw.Write(str);

    sw.Flush();

    sw.Close();

    returnValue = "yes";

     }

        catch(Exception e)

        {  returnValue = e.Message.ToString();

        }

        finally

        {   sdr.Close();

            conn.Close();

        }

        return returnValue;

}

--------------------------------------------------------------------------------------------
另外一个代码
--------------------------------------------------------------------------------------------
using   System;
using   System.Data;
using   System.Configuration;
using   System.Collections;
using   System.Web;
using   System.Web.Security;
using   System.Web.UI;
using   System.Web.UI.WebControls;
using   System.Web.UI.WebControls.WebParts;
using   System.Web.UI.HtmlControls;
using   Mysqlserver;
using   System.IO;
using   System.Text;
namespace   NewsAdd
{
        public   partial   class   Admin_AdminPanel_NewsAdd   :   System.Web.UI.Page
        {
                protected   void   Page_Load(object   sender,   EventArgs   e)
                {

                }
                protected   void   Button1_Click(object   sender,   EventArgs   e)
                {
                        string   strDate   =   DateTime.Now.ToString( "yyMMdd ")   +   "\ "   +   DateTime.Now.ToString( "yyyymmddhhmmss ");
                        string   strFileName   =   strDate   +   ".shtml ";
                        string   strTitle=Request.Form[ "Title "].ToString().Trim();
                        string   strContent=Request.Form[ "Content "].ToString().Trim();
                        string[]   content   =   strContent.Split(new   Char[]   ...{ ' ¦ '});
                        int   upbound   =   content.Length;
                        SqlServerDataBase   db   =   new   SqlServerDataBase();
                        bool   success   =   db.Insert( "insert   into   inNews(Title,Content,FilePath)values( ' "   +   strTitle   +   " ', ' "   +   strContent   +   " ', ' "   +   strFileName   +   " ') ",   null);

                        string   dir   =   Server.MapPath( "http://www.cnblogs.com/ "+ "NewsFiles/ "+DateTime.Now.ToString( "yyMMdd "));
                        if   (!Directory.Exists(dir))
                        {
                                Directory.CreateDirectory(dir);
                        }

                        try
                        {
                        for   (int   i   =   0;   i   <   content.Length;   i++)
                        {
                        //string[]   newContent   =   new   string[4];
                        StringBuilder   strhtml   =   new   StringBuilder();
                                using   (StreamReader   sr   =   new   StreamReader(Server.MapPath( "http://www.cnblogs.com/ "   +   "NewsFiles/ ")   +   "\template.html ",Encoding.GetEncoding( "gb2312 ")))
                                {
                                        String   oneline;
                                        while   ((oneline   =   sr.ReadLine())   !=   null)
                                        {
                                                strhtml.Append(oneline);
                                        }
                                        sr.Close();
                                }
                        DataSet   ds   =   db.Select( "select   top   1   NewsId   from   inNews   order   by   NewsId   desc ",   null);
                        string   strTable   =   " <table> <tr> <td> $upUrl </td> <td> $Number </td> <td> $downUrl </td> </tr> </table> ";
                                string   FilePath= " ";
                                strhtml   =   strhtml.Replace( "$Title ",   strTitle);
                                strhtml   =   strhtml.Replace( "$NewsId ",   ds.Tables[0].Rows[0][ "NewsId "].ToString());
                                strhtml   =   strhtml.Replace( "$Time ",   DateTime.Now.ToString( "yyyy/MM/dd "));
                                strhtml   =   strhtml.Replace( "$Content ",   content[i]);
                                string   strNumber   =   " ";
                                for   (int   m   =   1;   m   <=upbound;   m++)
                                {
                                        if   (m   ==   1)
                                                strNumber   =   strNumber   +   "   [ "+ " <a   href= "   +   "../ "   +   strDate   +   ".shtml "   +   "> "   +   m   +   " </a> "+ "]   ";
                                        else
                                        {
                                                int   n   =   m   -   1;
                                                strNumber   =   strNumber   +   "   [ "   + " <a   href= "   +   "../ "   +   strDate   +   "_ "   +   n   +   ".shtml "   +   "> "   +     m   +   " </a> "+ "]   ";
                                        }
                                }
                                        if   (upbound   ==   0)
                                        {
                                                FilePath   =   Server.MapPath( "http://www.cnblogs.com/ ")   +   "NewsFiles "   +   "// "   +   strDate   +   ".shtml ";
                                                strhtml   =   strhtml.Replace( "$Pager ",   " ");
                                        }
                                        else
                                        {
                                                if   (i   ==   0)
                                                        FilePath   =   Server.MapPath( "http://www.cnblogs.com/ ")   +   "NewsFiles "   +   "// "   +   strDate   +   ".shtml ";
                                                else
                                                        FilePath   =   Server.MapPath( "http://www.cnblogs.com/ ")   +   "NewsFiles "   +   "// "   +   strDate   +   "_ "   +   i   +   ".shtml ";
                                               
                                                if   (i   ==   0)
                                                        strTable   =   strTable.Replace( "$upUrl ",   " ");  

                                                if   (i   <=   1)//上一页分页
                                                        strTable   =   strTable.Replace( "$upUrl ",   " <a   href= "   +   "../ "   +   strDate   +   ".shtml "   +   "> 上一页 </a> ");
                                                else
                                                {
                                                        int   p   =   i   -   1;
                                                        strTable   =   strTable.Replace( "$upUrl ",   " <a   href= "   +   "../ "   +   strDate   +   "_ "   +   p   +   ".shtml "   +   "> 上一页 </a> ");
                                                }
                                               
                                                if(upbound==1)
                                                strTable   =   strTable.Replace( "$Number ",   " ");
                                                else
                                                strTable   =   strTable.Replace( "$Number ",   strNumber);
                                                if(i==upbound-1)
                                                        strTable   =   strTable.Replace( "$downUrl ",   " ");

                                                if   (i   !=   upbound   -   1)
                                                {
                                                        int   q   =   i   +   1;
                                                        strTable   =   strTable.Replace( "$downUrl ",   " <a   href= "   +   "../ "   +   strDate   +   "_ "   +   q   +   ".shtml "   +   "> 下一页 </a> ");
                                                }
                                                else
                                                {
                                                        int   j   =   upbound   -   1;
                                                        strTable   =   strTable.Replace( "$downUrl ",   " <a   href= "   +   "../ "   +   strDate   +   "_ "   +   j   +   ".shtml "   +   "> 下一页 </a> ");
                                                }

                                                strhtml   =   strhtml.Replace( "$Pager ",   strTable);
                                        }
                                FileInfo   finfo   =   new   FileInfo(FilePath);
                                using   (FileStream   fs   =   finfo.OpenWrite())
                                {
                                        StreamWriter   sw   =   new   StreamWriter(fs,   System.Text.Encoding.Default);
                                        sw.WriteLine(strhtml);
                                        sw.Flush();
                                        sw.Close();
                                }
                        }
                }
                catch   (Exception   err)
                {
                        Response.Write(err.ToString());
                }
                }
}
}

 

版权

作者:灵动生活 郝宪玮

出处:http://www.cnblogs.com/ywqu

如果你认为此文章有用,请点击底端的【推荐】让其他人也了解此文章,

img_2c313bac282354945ea179a807d7e70d.jpg

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

相关文章
|
开发框架 前端开发 JavaScript
ASP.NET Core静态文件的使用方法
静态文件(HTML,CSS,图片和Javascript之类的资源)会被ASP.NET Core应用直接提供给客户端。 静态文件通常位于网站根目录(web root) <content-root>/wwwroot文件夹下。通常会把项目的当前目录设置为Content root,这样项目的web root就可以在开发阶段被明确。
ASP.NET Core静态文件的使用方法
|
.NET
一起谈.NET技术,详解ASP.NET页面的aspx扩展
  需求:某网站因业务扩展,需拆分出另一个站点,新旧站点具有相同的内容,但具体栏目表现形式上不一样。原网站运行多年,有大量的图片,这些图片也会在新站上使用。任务是:   保证两个网站图片内容同步,即原来的站点增加一个图片,新站点即可使用这个图片。
937 0
|
.NET 开发框架
ASP.NET 基础多文件上传
////多图上传 [HttpPost] public string duo() { ///.net 多文件上传 获取文件的集合 HttpFileCollection files = HttpContext.
802 0
|
JavaScript 搜索推荐 前端开发