john.net

博客园 首页 新随笔 联系 订阅 管理

1.扩展方法必须在非泛型静态类中定义

2.扩展方法必须是静态的

3.不能在静态类中声明实例成员

public static class PubMethod
    {
        public static string GetIP(this Controller ctrl)
        {
            string ip;
            if (ctrl.HttpContext.Request.ServerVariables["HTTP_VIA"] != null)             {
                ip = ctrl.HttpContext.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();             }
            else            {
                ip = ctrl.HttpContext.Request.ServerVariables["REMOTE_ADDR"].ToString();             }
            return ip;

        }
    }

这样就可以在Action里面直接调用了 PubMethod.GetIP(this);

posted on 2009-05-20 13:05  john.net  阅读(1447)  评论(0编辑  收藏  举报