I would like to move the following piece of code from a c# aspx.cs file into a stand alone class.cs file.
string getIP = Request.ServerVariables[\"HTTP_X_FORWARD
Request is a property of the page class. Therefore you cannot access it from a "standalone" class.
However, you can get the HttpRequest anyway via HttpContext.Current
var request = HttpContext.Current.Request;
Note that this works even in a static method. But only if you're in a HttpContext(hence not in a Winforms application). So you should ensure that it's not null
:
if (HttpContext.Current != null)
{
var request = HttpContext.Current.Request;
}
Edit: Of course you can also pass the request as parameter to the method that consumes it. This is good practise since it doesn't work without. On this way every client would know immediately whether this class/method works or not.
The reason it doesn't work is because you cannot access server variables in a class library project.
You should avoid attempting to make this act like a web class and instead pass the information you need to the class object via a normal parameter.