title: Asp.NET MVC REST API: How to get Client Host Name
body: <p>I am developing REST API application in ASP.NET MVC3. I am trying to get client client HOST Name in action but cannot success. I am using this API in multiple applications so I want to log the domain name.</p>
  
  <p><a href="https://stackoverflow.com/questions/15712720/get-web-api-consumer-ip-address-and-hostname-in-asp-net-web-api">Link 1</a></p>
  
  <pre><code> HttpContext.Current.Request.UserHostAddress;
   System.Web.HttpContext.Current.Request.UserHostName;
   System.Web.HttpContext.Current.Request.UserHostAddress;
  </code></pre>
  
  <p><a href="https://stackoverflow.com/a/9571292/1558544">Link 2</a></p>
  
  <pre><code>private string GetClientIp(HttpRequestMessage request)
  {
      if (request.Properties.ContainsKey("MS_HttpContext"))
      {
          return ((HttpContextWrapper)request.Properties["MS_HttpContext"]).Request.UserHostAddress;
      }
      else if (request.Properties.ContainsKey(RemoteEndpointMessageProperty.Name))
      {
          RemoteEndpointMessageProperty prop;
          prop = (RemoteEndpointMessageProperty)request.Properties[RemoteEndpointMessageProperty.Name];
          return prop.Address;
      }
      else
      {
          return null;
      }
  }
  </code></pre>
  
  <p>All these options providing IP address but i want to get host name</p>
  
  <p><strong>API Controller</strong>  www.myapi.com</p>
  
  <pre><code>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;
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Calling API Action from different domain</strong> www.myapiconsume.com </p>
  
  <pre><code> 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);
          }
  
      }
  </code></pre>
  
  <p>How can i get this? </p>