MVC模式下要获取客户IP可以在ActionFilterAttribute中进行拦截

 filterContext.HttpContext.Request.UserHostAddress

同样,在WebAPI中也可以用同样的方式获取,只是继承自System.Web.Http.Filters.ActionFilterAttribute

 private string GetClientIP(HttpActionContext actionContext)
{
if (actionContext.Request.Properties.ContainsKey("MS_HttpContext"))
{
return ((HttpContextWrapper)actionContext.Request.Properties["MS_HttpContext"]).Request.UserHostAddress;
} if (actionContext.Request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
{
RemoteEndpointMessageProperty prop;
prop = (RemoteEndpointMessageProperty)actionContext.Request.Properties[RemoteEndpointMessageProperty.Name];
return prop.Address;
} return null;
}

最新文章

  1. 转 C# 只允许运行一个实例
  2. Android之自定义ViewGroup
  3. JavaScript实现样式表的简单切换
  4. Deep Learning for NLP 文章列举
  5. javascript封装自定义滚动条方法,可自定义四个边框滚动条
  6. [译]Java 设计模式之命令
  7. echarts仪表盘如何设置图例(legend)
  8. 一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——实现篇:(四)用户接口层之处理SDP报文
  9. [php]通过http post发送json数据
  10. 使用Netbeans内置的Git工具
  11. hdu1003 Max Sum---最大子段和+记录开始结束点
  12. Java基础 -- 连接字符串时,使用+还是StringBuilder
  13. optional install error: Package require os(darwin) not compatible with your platform(win32)
  14. Java中线程的同步问题
  15. 二 分析easyswoole源码(启动服务)
  16. Matconvnet 的一些记录
  17. mysql分区分表讲解
  18. ac1067
  19. ubuntu下screen的使用
  20. Codeforces Round #415 (Div. 2)C

热门文章

  1. 《nginx 二》深入理解nginx的各项配置
  2. HDU 5407——CRB and Candies——————【逆元+是素数次方的数+公式】
  3. web前端兼容性整理+完善
  4. 2.C#中的常用快捷键
  5. image压缩
  6. 修复kindEditor点击加粗, 内容焦点跳动的问题
  7. web worker技术-js新线程
  8. jQuery核心探究
  9. layui文档阅读进度
  10. tnagios - query handler