Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the User's IP Address in Castle MVC (Monorail)?

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)

like image 862
Brenton Alker Avatar asked Jan 23 '23 04:01

Brenton Alker


1 Answers

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;
like image 158
Ken Egozi Avatar answered Jan 29 '23 21:01

Ken Egozi