首页 新闻 会员 周边 捐助

生成静态网页

0
悬赏园豆:10 [已关闭问题]

各位大侠,在下有这样一个需求:由asp.net动态生成的aspx页面如何在保存成.htm文件后不用再连接数据库从.htm可生成.aspx页面的效果?请大家给思路呀,谢谢!

Qubook的主页 Qubook | 初学一级 | 园豆:0
提问于:2009-05-27 15:20
< >
分享
其他回答(3)
0

using System;

using System.Data;

using System.Configuration;

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 System.IO;

using System.Text;

public partial class WriteFile : System.Web.UI.Page

{

    public static bool createHtml(string[] strNewHtml, 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 = "";

        try

        {

            sr = new StreamReader(filepath, code);

            s = sr.ReadToEnd();

        }

        catch (Exception ex)

        {

            throw ex;

        }

        finally

        {

            sr.Close();

        }

        try

        {

            for (int i = 0; i < strNewHtml.Length; i++)

            {

                s = s.Replace(stroldHtml[i], strNewHtml[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[] strNewHtml, 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 = "";

        try

        {

            ReaderFile = new StreamReader(FilePath, Code);

            strFile = ReaderFile.ReadToEnd();

        }

        catch (Exception ex)

        {

            throw ex;

        }

        finally

        {

            ReaderFile.Close();

        }

        try

        {

            int intLengTh = strNewHtml.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, strNewHtml[i]);

            }

            WrirteFile = new StreamWriter(FilePath, false, Code);

            WrirteFile.Write(strFile);

            Flage = true;

        }

        catch (Exception ex)

        {

            throw ex;

        }

        finally

        {

            WrirteFile.Flush();

            WrirteFile.Close();

        }

        return Flage;

    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        string NewTitle = "C#将ASPX页面转换为静态页面";

        string NewAuthor = "佚名";

        string NewContent = "秦迷修订版本";

        DateTime PubTime = DateTime.Now;

        string[] strNewHtml = new string[] { NewTitle, NewAuthor, NewContent, PubTime.ToString() };

        string[] strOldHtml = new string[] { "@Title", "@Author", "@Content", "@PubTime" };

        string strDirName = DateTime.Now.ToString("yyyyMM");

        string strFileName = DateTime.Now.ToString("yyMMddhhmmss") + ".html";

        Directory.CreateDirectory(MapPath(strDirName));

        string strFilePath = string.Format(strDirName + "/{0}", strFileName);

        try

        {

            if (WriteFile.createHtml(strNewHtml, strOldHtml, "qinmi.html", strFilePath))

            {

                Label1.Text = "生成成功!";

            }

            else

            {

                Label1.Text = "生成失败!";

            }

        }

        catch

        {

            Label1.Text = "生成失败!";

        }

    }

}

------------qinmi.html模板页--------------

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Test</title>

    <style type="text/css">

    .tb{border: solid 1px #CCCCCC;}

    .td{border-bottom:dotted 1px #CCCCCC;}

    </style>

</head>

<body>

    <table class="tb">

        <tr>

            <td class="td">

                @Title</td>

        </tr>

        <tr>

            <td class="td">

                作者:@Author 发布时间:@PubTime

            </td>

        </tr>

        <tr>

            <td>

                @Content</td>

        </tr>

    </table>

</body>

</html>

梦寻千里 | 园豆:250 (菜鸟二级) | 2009-05-27 15:57
0

别太看懂,可以在.htm页面里面欠一个aspx页面

尹成亮 | 园豆:210 (菜鸟二级) | 2009-05-27 15:58
0

参见 http://space.cnblogs.com/question/7061/

---------------------------------------------------------------------------------------------

静态化过程的难度和动态页面“动态”因素成指数关系。如下是我最近应用而写的一个静态化处理抽象类BaseHandlerStaticizer:


using System;
using System.IO;
using System.Text;
using System.Web;

/// <summary>
/// 提供抽象静态化服务.
/// Howard 20090420.
/// </summary>
public abstract class BaseHandlerStaticizer : IHttpHandler {

private const int ScriptTimeout = 60 * 5;// 5 分钟

private HttpContext _context;
protected HttpContext Context {
get { return _context; }
private set {
_context
= value;
}
}

/// <summary>
/// 待处理的需要静态化的Url
/// </summary>
protected abstract string[] ProcessUrls {
get;
}

protected abstract bool PreserveForm {
get;
}

protected void Process() {
Context.Server.ScriptTimeout
= ScriptTimeout;
StringBuilder builder;
foreach (string url in ProcessUrls) {
builder
= new StringBuilder();
using (TextWriter tw = new StringWriter(builder)) {
Context.Server.Execute(url, tw, PreserveForm);
SaveStaticFile(url, builder.ToString());
}
}
}

/// <summary>
/// 保存静态文件
/// </summary>
/// <param name="contents"></param>
/// <param name="url"></param>
protected abstract void SaveStaticFile(string sourceUrl, string contents);

#region IHttpHandler 成员

bool IHttpHandler.IsReusable {
get { return false; }
}

void IHttpHandler.ProcessRequest(HttpContext context) {
try {
Context
= context;
Process();
context.Response.ContentType
= "text/plain";
context.Response.Write(
"Ok.");
}
catch (Exception E) {
context.Response.ContentType
= "text/plain";
context.Response.Write(E.ToString());
}
}

#endregion

}

主要的静态化方法是 Process 函数, 如下:

 

Process
protected void Process() {
Context.Server.ScriptTimeout
= ScriptTimeout;
StringBuilder builder;
foreach (string url in ProcessUrls) {
builder
= new StringBuilder();
using (TextWriter tw = new StringWriter(builder)) {
Context.Server.Execute(url, tw, PreserveForm);
SaveStaticFile(url, builder.ToString());
}
}
}
将你的“动态”参数变为 querystring 形式,枚举各参数值调用 Context.Server.Execute 静态化你的动态页面,最后保存该静态页面为文件即可(扩展名可为 .htm 等)。注意保存为静态文件时的命名规则,比如 Default_1_1.htm, Default_1_2.htm 等。

陛下 | 园豆:3938 (老鸟四级) | 2009-05-27 17:25
0

月经问

风海迷沙 | 园豆:4453 (老鸟四级) | 2009-05-29 10:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册