C# Asp.NET MVC REST API:如何获取客户端主机名

C# Asp.NET MVC REST API:如何获取客户端主机名,c#,asp.net-mvc,asp.net-mvc-3,asp.net-web-api,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Web Api,我正在ASP.NETMVC3中开发RESTAPI应用程序。我正在尝试使用客户端主机名,但无法成功。我在多个应用程序中使用此API,因此我想记录域名 所有这些选项都提供IP地址,但我想获取主机名 API控制器www.myapi.com public class TESTController : ApiController { public TESTController() { } [HttpGet] public object Add(string d

我正在ASP.NETMVC3中开发RESTAPI应用程序。我正在尝试使用客户端主机名,但无法成功。我在多个应用程序中使用此API,因此我想记录域名

所有这些选项都提供IP地址,但我想获取主机名

API控制器www.myapi.com

public class TESTController : ApiController
{
    public TESTController()
    {
    }

    [HttpGet]
    public object Add(string data = "")
    {
        try
        {
            string result = "0";

            if (!string.IsNullOrEmpty(data))
            {
                //should be www.myapiconsume.com
                var host = System.Web.HttpContext.Current.Request.UserHostName;
                var ip = System.Web.HttpContext.Current.Request.UserHostAddress;
            }

            return new { response = result };
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }
}
从不同域调用API操作www.myapiconsume.com

 public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var url = "http://myapi.com/api/test/add?data={0}";
            url = string.Format(url, "{ data}");
            WebClient client = new WebClient();
            var result = client.DownloadString(url);
            return Content(result);
        }

    }

我怎么能得到这个

不幸的是,它不是默认提供的;如果您在同一网络上,您可以尝试:

var result = System.Net.Dns.GetHostEntry(System.Web.HttpContext.Current.Request.ServerVariables("remote_addr"));

但是,如果客户端是通过代理来的,那么这也可能会有问题。

更新调用操作:

var url = "http://myapi.com/api/test/add?data={0}";
url = string.Format(url, "{ data}");
WebClient client = new WebClient();
client.Headers.Add("Referer", Request.Url.AbsoluteUri);
var result = client.DownloadString(url);
return Content(result);
然后在目标服务器上:

Request.UrlReferrer.Host
如果您的服务器具有公共ip,则您可以尝试解决该问题:

Dns.GetHostEntry(HttpContext.Current.Request.UserHostAddress)
您可以使用方法反向查找

public static string ReverseLookup(string ip)
{
    if (String.IsNullOrWhiteSpace(ip))
        return ip;

    try
    {
        IPHostEntry host = Dns.GetHostEntry(ip);

        return host != null ? host.HostName : ip;
    }
    catch (SocketException)
    {
        return ip;
    }
}

它提供了服务器名,但我想要域名。这两个应用程序托管在同一台服务器上,但网站不同。提供了托管API的域名,但我想获取此API使用的客户端域名。我的API托管在www.myapi.com上,我从www.consumerapi调用它。com@AskQuestion您想获得www.consumapi.com吗?是的,www.consumapi.com应该在主机变量中。我更新了一个问题:您是否也在开发客户端应用程序?我认为Dns.GetHostEntry(ip)没有选择方法。我说的对吗?奇怪的是它显示了服务器托管空间的名称。作为cpp.blue.sitehosted.com。此函数显示“蓝色”。DNS本身是一个复杂的主题-转发、重定向、内部和外部。一旦发布到生产环境(使用公共IP),与开发机器(使用内部IP)相比可能会有所不同。然而,基于IP地址查找DNS并不是100%可靠的。我的建议是您的应用程序不要依赖主机名。
Dns.GetHostEntry(HttpContext.Current.Request.UserHostAddress)
public static string ReverseLookup(string ip)
{
    if (String.IsNullOrWhiteSpace(ip))
        return ip;

    try
    {
        IPHostEntry host = Dns.GetHostEntry(ip);

        return host != null ? host.HostName : ip;
    }
    catch (SocketException)
    {
        return ip;
    }
}