我正在使用ASP.NET Web Api来公开一些GET方法。
@H_404_2@但是在我返回数据之前,我需要将几个细节记录到数据库,其中很少数据如下所示:
@H_404_2@>来电者的Ip
>来电者的用户代理
> Caller’s Used Url @H_404_2@现在在控制器当我以前做这个我曾经使用以下代码,
>来电者的用户代理
> Caller’s Used Url @H_404_2@现在在控制器当我以前做这个我曾经使用以下代码,
var ipAddress = Request.ServerVariables["REMOTE_ADDR"]; var userAgent = Request.UserAgent;@H_404_2@但是在Web API中我无法使用这个。 @H_404_2@任何人都可以帮助我。
解决方法
我想到了,
&安培; @H_404_2@Get the IP address of the remote host
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; }@H_404_2@有一点帮助 @H_404_2@Get Web Api consumer IP Address and HostName in ASP.net C#
&安培; @H_404_2@Get the IP address of the remote host