我正在开发ASP.NET MVC3中的REST API应用程序。我尝试在操作中获取客户端主机名,但没有成功。由于我在多个应用程序中使用此API,因此我想记录域名。
链接1: Link 1
所有这些选项都提供了IP地址,但我想获取主机名。 API控制器 www.myapi.com
链接1: Link 1
HttpContext.Current.Request.UserHostAddress;
System.Web.HttpContext.Current.Request.UserHostName;
System.Web.HttpContext.Current.Request.UserHostAddress;
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;
}
}
所有这些选项都提供了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);
}
}
我该如何获得这个?