如何获得Ip地址和用户代理中的ASP.NET Web API获取方法 [英] How to get IpAddress and UserAgent in ASP.NET Web API get methods

查看:205
本文介绍了如何获得Ip地址和用户代理中的ASP.NET Web API获取方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的ASP.NET Web API来揭露一些GET方法。

不过,我返回的数据之前,我需要登录几个细节的分贝,其中他们几个都低于所列:


  • 呼叫者的IP

  • 呼叫者的用户代理

  • 来电的二手网址

现在,当我用来做这个我用下面的code控制器,

  VAR ip地址= Request.ServerVariables [REMOTE_ADDR];
VAR的userAgent = Request.UserAgent;

但在这里的Web API,我无法用这个。

任何人都可以请帮我了这一点。


解决方案

我想通了,

 公共静态LogModel GetApiLogDetails()
{
    VAR logModel =新LogModel();
    logModel.TimeStamp = DateTime.Now;
    logModel.CallerIp = HttpContext.Current.Request.UserHostAddress;
    logModel.CallerAgent = HttpContext.Current.Request.UserAgent;
    logModel.CalledUrl = HttpContext.Current.Request.Url.OriginalString;
    返回logModel;
}

从一点点帮助。

<一个href=\"http://stackoverflow.com/questions/15712720/get-web-api-consumer-ip-address-and-hostname-in-asp-net-c-sharp\">Get网页API消费者的IP地址和主机名在ASP.net C#
&安培;

得到远程主机的

I am using ASP.NET Web Api to expose a few GET methods.

But before I return the data I need to log a couple of details to the db, of which few of them are as listed below :

  • Caller's Ip
  • Caller's User Agent
  • Caller's Used Url

Now in the controller when I used to do this I used to use the following code,

var ipAddress = Request.ServerVariables["REMOTE_ADDR"];
var userAgent = Request.UserAgent;

But here in Web API I am unable to use this.

Can anyone please help me out with this.

解决方案

I figured it out,

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

with a little help from

Get Web Api consumer IP Address and HostName in ASP.net C# &

Get the IP address of the remote host

这篇关于如何获得Ip地址和用户代理中的ASP.NET Web API获取方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆