利用asp.Netpage Render静態化ページ

5716 ワード

ページは次のように継承されます.
using System;
using System.IO;
using System.Web.UI;

/// <summary>
///CacheHtmlPage      
/// </summary>
public class StaticFileCachePageBase : System.Web.UI.Page
{
    bool IsValidCacheFile = false;          //        
    string CacheFilePath = string.Empty;    //      

    string CacheFolder = "/Cache/";         //          


    const int CacheSeconds = 3600;            //         



    string encoding = "gb2312";

    void ClearTimeOutFile(string folder)
    {
        DateTime now = DateTime.Now;
        DirectoryInfo di = new DirectoryInfo(folder);
        FileInfo[] fis = di.GetFiles();
        foreach (FileInfo file in fis)
        {
            TimeSpan ts = now - file.LastWriteTime;

            if (ts.TotalSeconds > CacheSeconds)
            {
                file.Delete();
            }
        }
    }


    protected override void OnPreInit(EventArgs e)
    {

        string code = Encryption.Escape(Request.RawUrl);
        string folder = CacheFolder + code[0] + "/" + code[1] + "/";

        CacheFilePath = Server.MapPath(folder + code + ".html");

        if (File.Exists(CacheFilePath))
        {
            TimeSpan ts = DateTime.Now - File.GetLastWriteTime(CacheFilePath);

            if (ts.TotalSeconds <= CacheSeconds)
            {
                IsValidCacheFile = true;

                Response.ContentEncoding = System.Text.Encoding.GetEncoding(encoding);
                
                Response.TransmitFile(CacheFilePath);
                return;

            }
        }

        ClearTimeOutFile(folder);

        base.OnPreInit(e);
    }

    protected override void Render(HtmlTextWriter writer)
    {


        if (!IsValidCacheFile)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(sw);


            base.Render(htmlTextWriter);
            htmlTextWriter.Flush();
            htmlTextWriter.Close();

            string pageContent = sw.ToString();
            Response.Write(pageContent);

            string folder = Path.GetDirectoryName(CacheFilePath);
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            using (var fs = File.Create(CacheFilePath))
            {
                byte[] data = System.Text.Encoding.GetEncoding(encoding).GetBytes(pageContent);

                fs.Write(data, 0, data.Length);
                fs.Close();
            }

        }

    }
}
using System;
using System.Text;


public class Encryption
{
    public static string Escape(string content)
    {
        string base64 = EncodeBase64(content);
        string reverseString = ReverseString(base64);
        string crossString = EncodeCrossString(reverseString, 3);


        return crossString;
    }

    public static string UnEscape(string content)
    {
        string decodeCrossString = DecodeCrossString(content, 3);
        string reverseString = ReverseString(decodeCrossString);
        return DecodeBase64(reverseString);
    }



    public static string DecodeBase64(string str)
    {
        try
        {
            byte[] bytes = Convert.FromBase64String(str);
            return Encoding.Default.GetString(bytes);
        }
        catch
        {
            return str;
        }
    }

    public static string DecodeCrossString(string str, int bitNum)
    {
        int num3;
        StringBuilder builder = new StringBuilder();
        int num = str.Length % bitNum;
        int num2 = (str.Length - num) / bitNum;
        string str2 = str.Substring(str.Length - num);
        string[] strArray = new string[num2];
        for (num3 = 0; num3 < num2; num3++)
        {
            strArray[num3] = str.Substring(num3 * bitNum, bitNum);
        }
        string str3 = strArray[num2 - 1];
        for (num3 = num2 - 1; num3 > 0; num3--)
        {
            strArray[num3] = strArray[num3 - 1];
        }
        strArray[0] = str3;
        for (num3 = 0; num3 < num2; num3++)
        {
            strArray[num3] = strArray[num3].Substring(1) + strArray[num3].Substring(0, 1);
            builder.Append(strArray[num3]);
        }
        builder.Append(str2);
        return builder.ToString();
    }

    public static string EncodeBase64(string str)
    {
        return Convert.ToBase64String(Encoding.Default.GetBytes(str));
    }

    public static string EncodeCrossString(string str, int bitNum)
    {
        int num3;
        StringBuilder builder = new StringBuilder();
        int num = str.Length % bitNum;
        int num2 = (str.Length - num) / bitNum;
        string str2 = str.Substring(str.Length - num);
        string[] strArray = new string[num2];
        for (num3 = 0; num3 < num2; num3++)
        {
            strArray[num3] = str.Substring(num3 * bitNum, bitNum);
        }
        string str3 = strArray[0];
        for (num3 = 0; num3 < (num2 - 1); num3++)
        {
            strArray[num3] = strArray[num3 + 1];
        }
        strArray[num2 - 1] = str3;
        for (num3 = 0; num3 < num2; num3++)
        {
            strArray[num3] = strArray[num3].Substring(bitNum - 1) + strArray[num3].Substring(0, bitNum - 1);
            builder.Append(strArray[num3]);
        }
        builder.Append(str2);
        return builder.ToString();
    }

    public static string ReverseString(string str)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = str.Length - 1; i >= 0; i--)
        {
            builder.Append(str[i]);
        }
        return builder.ToString();
    }

}