我正在使用ASP.NET Web Api来公开一些GET方法。
但是在我返回数据之前,我需要将几个细节记录到数据库,其中很少数据如下所示:
>来电者的Ip
>来电者的用户代理
> Caller’s Used Url
现在在控制器当我以前做这个我曾经使用以下代码,
var ipAddress = Request.ServerVariables["REMOTE_ADDR"]; var userAgent = Request.UserAgent;
但是在Web API中我无法使用这个。
任何人都可以帮助我。
解决方法
我想到了,
public static LogModel GetApiLogDetails() { var logModel = new LogModel(); logModel.TimeStamp = DateTime.Now; logModel.CallerIp = HttpContext.Current.Request.UserHostAddress; logModel.CallerAgent = HttpContext.Current.Request.UserAgent; logModel.CalledUrl = HttpContext.Current.Request.Url.OriginalString; return logModel; }
有一点帮助
Get Web Api consumer IP Address and HostName in ASP.net C#
&安培;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。