My solution is similar to the user1587439 answer, but works directly on the controller instance (instead of accessing HttpContext.Current).
In the Watch window, I saw that this.RequestContext.WebRequest contains the UserHostAddress property, but since it uses the WebHostHttpRequestContext type (which is internal to the "System.Web.Http" assembly) - I could not access it directly, so I used reflection for direct access to it:
string hostAddress = ((System.Web.HttpRequestWrapper)this.RequestContext.GetType().Assembly.GetType("System.Web.Http.WebHost.WebHostHttpRequestContext").GetProperty("WebRequest").GetMethod.Invoke(this.RequestContext, null)).UserHostAddress;
I do not think this is the best solution. using reflection may cause problems in the future if the framework is updated (due to name changes), but for my needs it is ideal
Nissim Aug 07 '17 at 9:01 on 2017-08-07 09:01
source share