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);

相关文章:

  • 2021-10-29
  • 2021-10-10
  • 2022-12-23
  • 2022-01-06
  • 2022-12-23
猜你喜欢
  • 2021-06-18
  • 2022-12-23
  • 2021-08-01
  • 2022-01-24
  • 2021-07-27
  • 2021-05-15
相关资源
相似解决方案