日期:2014-05-18  浏览次数:20497 次

看在我发了第四个帖的份儿上,就帮帮我吧,解决后给100分,网站计数器问题
页面已经做完了
现在要加入一个新功能

用户访问计数器
只要用户打开页面就计数
刷新也是
而且后台能修改计数器数字

我找了一天的代码,都不行
只有统计的(统计的我做的差不多了),没有能修改的
都是用application
而application我不是很懂
不知道怎么改

谁帮我解决一下啊
解决后我会加分的

------解决方案--------------------
这里是一个demo。你可以创建一个.cs文件(例如叫做BLL.cs)
C# code

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");     //注意:这里并没有检查路径中的字符合法性。
    }
}