spence0021
spence0021

Reputation: 61

Using ASP.NET Response and Request objects in a web client class

I currently have a webclient class that maintains session for my web application using session variables but I want to instead use cookies to maintain session. I of course don't have access to the ASP.NET Response and Request variables in this class. Would I have to pass those objects to the webclient class?

Upvotes: 1

Views: 3279

Answers (2)

Element
Element

Reputation: 4031

As Yuriy pointed out you could access the request/response objects directly via the HttpContext.Current namespace, this however is bad practice. Your class has a dependency on the request/response objects, these should be passed into your class via it's constructor.

e.g.

public class SessionExample{


    public SessionExample(System.Web.HttpRequest request, System.Web.HttpResponse response){

    }

}

Or if your class is meant to live for longer than the duration of a single http request you can pass them in as method paramaters

public class SessionExample{


    public SessionExample(){

    }

    public void DoSomething(System.Web.HttpRequest request, System.Web.HttpResponse response){

    }

}

Structuring your code this way makes it more testable and will save you headaches down the road.

Upvotes: 1

suff trek
suff trek

Reputation: 39777

Not sure what are you trying to achieve, but in any of your custom classes inside of ASP.NET application you can access Request and Response via

HttpContext.Current.Request

and

HttpContext.Current.Response

Upvotes: 2

Related Questions