c# - Asp.NET MVC REST API: How to get Client Host Name -
i developing rest api application in asp.net mvc3. trying client client host name in action cannot success. using api in multiple applications want log domain name.
link 1
httpcontext.current.request.userhostaddress; system.web.httpcontext.current.request.userhostname; system.web.httpcontext.current.request.userhostaddress;
link 2
private string getclientip(httprequestmessage request) { if (request.properties.containskey("ms_httpcontext")) { homecoming ((httpcontextwrapper)request.properties["ms_httpcontext"]).request.userhostaddress; } else if (request.properties.containskey(remoteendpointmessageproperty.name)) { remoteendpointmessageproperty prop; prop = (remoteendpointmessageproperty)request.properties[remoteendpointmessageproperty.name]; homecoming prop.address; } else { homecoming null; } }
all these options providing ip address want host name
api controller www.myapi.com
public class testcontroller : apicontroller { public testcontroller() { } [httpget] public object add(string info = "") { seek { string result = "0"; if (!string.isnullorempty(data)) { //should www.myapiconsume.com var host = system.web.httpcontext.current.request.userhostname; var ip = system.web.httpcontext.current.request.userhostaddress; } homecoming new { response = result }; } grab (exception ex) { throw ex; } } }
calling api action different domain 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); homecoming content(result); } }
how can this?
it isn't supplied default, unfortunately; if you're on same network, can try:
var result = system.net.dns.gethostentry(system.web.httpcontext.current.request.servervariables("remote_addr"));
but may have issues if client coming through proxy, etc.
c# asp.net-mvc asp.net-mvc-3 asp.net-web-api
No comments:
Post a Comment