views:

311

answers:

1

hi guys, i'm totally new to the asp.net mvc stack and i was wondering what happened to the simple Page object and the Request ServerVariables object?

basically what i wanted to do is to pull out the client's pc ip address. but i fail to understand how the current MVC structure has changed all of this.

as far as i can understand, most of the variable object has been replaced by the HttpRequest variants?

anybody care to share some resources? really a sea of stuff to learn in the asp.net mvc world :)

For example i have a static class with this current function. how do i get the same result using asp.net mvc?

public static int getCountry(Page page)
    {
        return getCountryFromIP(getIPAddress(page));
    }

    public static string getIPAddress(Page page)
    {
        string szRemoteAddr = page.Request.ServerVariables["REMOTE_ADDR"];
        string szXForwardedFor = page.Request.ServerVariables["X_FORWARDED_FOR"];
        string szIP = "";

        if (szXForwardedFor == null)
        {
            szIP = szRemoteAddr;
        }
        else
        {
            szIP = szXForwardedFor;
            if (szIP.IndexOf(",") > 0)
            {
                string [] arIPs = szIP.Split(',');

                foreach (string item in arIPs)
                {
                    if (!isPrivateIP(item))
                    {
                        return item;
                    }
                }
            }
        }
        return szIP;
    }

And how do i call this function from the controller page? thanks.

+2  A: 

Request.ServerVariables["REMOTE_ADDR"] should work - either directly in a view or in the controller action method body (Request is a property of Controller class in MVC, not Page).

ovolko
how do i call this from the controller side?
melaos
See update. Just write Request.ServerVariables["..."] etc
ovolko
lol, hey that works, what happen if i wish to put that into a class object as above? and do i still need the page object?
melaos
I think you could use HttpContext.Current.Request
ovolko