views:

195

answers:

2

In a controller action of a CastleMVC application, how can I get the user's IP Address?

I think in asp.net mvc it would be Request.ServerVariables["REMOTE_ADDR"], but I can't find an equivalent in Castle.

(I am aware of potential proxy issue's etc, the address that is reported in the request is fine)

+3  A: 

I believe its:

HttpContext.Request.ServerVariables["REMOTE_ADDR"]
Patrick Steele
+3  A: 

Castle Monorail, as well as ASP.NET MVC, serve as an elegant MVC skin over the ASP.NET runtime.

As such, everything that can be done with the ASP.NET runtime (except for WebForms specific stuff like ViewState) can also be done with ASP.NET MVC and with Monorail.

So, you can always grab the current ASP.NET's HttpContext using the static HttpContext.Current method.

From Monorail, you can also use the IEngineContext's .UnderlyingContext property to access the ASP.NET HttpContext.

Specifically, in Monorail, you can grab the client's reported IP using the convenience property UserHostAddress on the current IRequest.

e.g. within a controller's action:

var clientIP = Request.UserHostAddress;
Ken Egozi