I'm building a Web.Api (mvc & web.api) and try to get the clients ip adress with this code..
[System.Web.Http.HttpPut]
public HttpResponseMessage Update([FromBody] BookInformation bookStatus)
{
// Stuff...
// Retrieve clients IP#
var clientIp = (System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"]).Request.UserHostAddress
}
But I get this error:
'System.Web.HttpRequestBase' does not contain a definition for 'Properties' and no extension method 'Properties' accepting a first argument of type 'System.Web.HttpRequestBase' could be found (are you missing a using directive or an assembly reference?)
What am I missing here?
According to the exception message it is saying that
Request
is of typeSystem.Web.HttpRequestBase
andProperties
does not exist, which would be accurate for that class. This looks like you are mixing MVC and WebApi Controllers.the
System.Web.HttpRequestBase Request
you are referencing is part of MVCController.Request
but your method structure looks like it is suppose to be part of a Web ApiSystem.Web.Http.ApiController
which has aSystem.Web.Http.HttpRequestMessage Request
property as well.Make sure you are inheriting from the correct controller.
I've used this extension helper to get the client ip with an
ApiController
And used like this