john.net

博客园 首页 新随笔 联系 订阅 管理
  5 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

公告

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 阅读(324) 评论(0) 编辑 收藏