分享

asp.net生成html静态页的多种方法

 wangn 2010-09-16
用C#做脚本的asp.net的方法,这个是我自己写的,在《Visual C#.NET范例入门与提高》的P311,有对WebRequest和HttpRequest、HttpWebRequest、HttpWebResponse四个类的简单说明
private bool CreateList(string url, string fna)
    {
        bool ok;
        //准备生成
        string strHtml;
        StreamReader sr = null; //用来读取流
        StreamWriter sw = null; //用来写文件
        Encoding code = Encoding.GetEncoding("utf-8"); //定义编码

        //构造web请求,发送请求,获取响应
        WebRequest HttpWebRequest = null;
        WebResponse HttpWebResponse = null;
        HttpWebRequest = WebRequest.Create(url);
        HttpWebResponse = HttpWebRequest.GetResponse();

        //获得流
        sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
        strHtml = sr.ReadToEnd();

        //写入文件
        try
        {
            sw = new StreamWriter(fna, false, code);
            sw.Write(strHtml);
            sw.Flush();
            ok = true;
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("<p>写入文件出错:" + ex.Message);
            HttpContext.Current.Response.End();
            ok = false;
        }
        finally
        {
            sw.Close();
        }
        return ok;
    }

调用的时候这样:

//要生成html页面的aspx页面
        string url = @"http://localhost/list.aspx";
        //html页面文件名
        string fna = Server.MapPath("") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";
        if (CreateList(url, fna))
        {
            Response.Write("<p>生成文件成功:" + fna);
        }

第二种方法是用一个html模板生成一个html页面,模版里面有对应的标签,可以从数据库和别的地方取数据,填写这个标签,生成一个html页面,这个方法在很多新闻系统里有用到

我参考这里面的代码写的:http://www./web/net/201/065118272748882.html

private string CreateDetailPage(string EventID,string EventTitle, string EventBody, string EventTime, string EventStat)
    {
        //模版所有路径、模版文件名、生成的文件路径、生成的文件名
        string path, temp, htmlfilepath,htmlfilename;
        path = Server.MapPath("");
        temp = Server.MapPath("testhtml.htm");
        htmlfilepath = path;
        htmlfilename = DateTime.Now.ToString("yyyyMMddHHmmss") + DateTime.Now.Millisecond.ToString() + ".html";

        //读模版
        Encoding code = Encoding.GetEncoding("gb2312");

        StreamReader sr = null;
        StreamWriter sw = null;
        string str = "";

        try
        {
            sr = new StreamReader(temp, code);
            str = sr.ReadToEnd(); // 读取文件
        }
        catch (Exception exp)
        {
            HttpContext.Current.Response.Write("<p>读取文件出错:" + exp.Message);
            HttpContext.Current.Response.End();
            sr.Close();
        }

        // 替换内容
        // 对应模版里的设置要修改
        str = str.Replace("re_symbol_EventID", EventID);
        str = str.Replace("re_symbol_EventTitle", EventTitle);
        str = str.Replace("re_symbol_EventBody", EventBody);
        str = str.Replace("re_symbol_EventTime", EventTime);
        str = str.Replace("re_symbol_EventStat", EventStat);

        // 写文件
        try
        {
            sw = new StreamWriter(htmlfilepath + "\\" + htmlfilename, false, code);
            sw.Write(str);
            sw.Flush();
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Write("<p>写入文件出错:" + ex.Message);
            HttpContext.Current.Response.End();
        }
        finally
        {
            sw.Close();
        }
        return htmlfilename;
    }

调用的时候这样:

//取内容,这里我取了页面上的一个gridview里的选中行的数据
        int i;
        i = GridView1.SelectedIndex;
        if (i == null || i==-1) i = 0;
        string EventID, EventTitle, EventBody, EventTime, EventStat;
        EventID=GridView1.Rows[i].Cells[0].Text;
        EventTitle=GridView1.Rows[i].Cells[1].Text;
        EventBody=GridView1.Rows[i].Cells[2].Text;
        EventTime=GridView1.Rows[i].Cells[3].Text;
        EventStat=GridView1.Rows[i].Cells[4].Text;
       
        //生成文件,返回文件名
        string fna;
        fna=CreateDetailPage(EventID, EventTitle, EventBody, EventTime, EventStat);
        Response.Write("<p>生成文件成功:" + fna);

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多