日期:2014-05-18 浏览次数:20630 次
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
public class BLL
{
static public int 网页计数
{
get
{
string key = GetKey();
object x = HttpContext.Current.Cache[key];
if (x == null)
{
string path = GetFileFullName(key);
if (!File.Exists(path))
x = 0;
else
x = int.Parse(File.ReadAllText(path));
HttpContext.Current.Cache.Insert(key, x, new CacheDependency(path));
}
return (int)x;
}
set
{
string key = GetKey();
File.WriteAllText(GetFileFullName(key), value.ToString());
HttpContext.Current.Cache.Remove(key);
}
}
static private string GetKey()
{
Page p = HttpContext.Current.Handler as Page;
return "页面{" + p.Title + "}的计数器";
}
static private string GetFileFullName(string key)
{
return HttpContext.Current.Server.MapPath("~/App_Data/" + key + ".txt"); //注意:这里并没有检查路径中的字符合法性。
}
}