Response and Request in a class

starwiz

Member
Joined
Jul 2, 2003
Messages
21
Im trying to write a class that will encapsulate a bunch of response and request calls Im doing. In the class, I deal with querystrings, read and write cookies, and will eventually be doing some SQL stuff...

Right now, Im having a problem with using response and request in the class.

Originally, I wrote the class so that after it was created, I could change the request and response objects through properties. The code would look like this from calling the class:
Code:
Dim Refs as new Referrences
Refs.response = response
Refs.request = request
Those two properties would set private data members inside the class equal to response and request.

This didnt work.

Any responses or requests I made would work until I left the page. So, for example, I could write and read cookies, so long as both the writing and reading were done on the same page.

After this didnt work, I decided to try making my class inherit system.web.ui.usercontrol. In my new sub, I call mybase.new, so the object should be all created. When I run my program, however, and try to make references to the response or request objects which should be included with the system.web.ui.usercontrol object, I get the error "Object reference not set to an instance of an object." It doesnt matter if I try and reference request in the class by "request," "me.request," or "mybase.request"; the error is the same.

I also tried, in my new sub, setting the private myRequest and myResponse members to System.Web.HttpContext.Current.Request/Response. This has the same effect as my first attempt.

Im sure Im doing something wrong...Ive never written something like this before, and I really have little idea what Im doing. :D

The question, however, is: what?

Can anyone help me?
 
The whole class is too big to post here...

Here are some pieces:
Code:
Public Structure CookieInfo
  Dim Keyword As String
  Dim SearchEngine As String
  Dim ReferringURL As String
  Dim ReferringWebsite As String
End Structure

Public Class Referrals

I reference some constants...their values really arent important...just know that theyre here

Ive tried this a number of ways, all yeilding the same results:

  Protected myRequest As System.Web.HttpRequest
  Protected myResponse As System.Web.HttpResponse

  Public Sub New()
    myRequest = System.Web.HttpContext.Current.Request
    myResponse = System.Web.HttpContext.Current.Response
  End Sub

Or just substituting myRequest and myResponse with System.Web.HttpContext.Current.Request/Response.

I also tried this...without the new statement but with the protected members:

  Public Function SetRequest(ByRef Value As System.Web.HttpRequest)
  	System.Web.HttpContext.Current.Request = Value
  End Function

  Public Function SetResponse(ByRef Value As System.Web.HttpResponse)
  	System.Web.HttpContext.Current.Response = Value
  End Function

and in my calling code, I did (class).setrequest(request) and (class).setresponse(response)...again, same effect.

  Public ReadOnly Property HasCookie() As Boolean
    Get
      If Not HasRequest Then
        Throw New System.Exception("Request property must not be nothing")
        Exit Property
      End If

      If System.Web.HttpContext.Current.Request.Cookies.Item(CookieName) Is Nothing Then
        Return False
      Else
        Return True
      End If
    End Get
  End Property

The cookie seems to be deleted after I move to another page...

  Public Sub WriteCookie()
    writes a cookie with all given information
    If Not HasResponse Then
      Throw New System.Exception("Response property must not be nothing")
      Exit Sub
    End If

    Dim Cookie As New System.Web.HttpCookie(CookieName)
    Cookie.Domain = CookieDomain
    Cookie.Expires = DateTime.Now.AddDays(CookieDays)

    Cookie.Values.Add(CookieSearchEngineName, SearchEngine)
    CookieSearchEngineName is a constant
    SearchEngine is a property that gets its value from a querystring...the same applies for everything else.
    Cookie.Values.Add(CookieReferringURLName, ReferringURL)
    Cookie.Values.Add(CookieKeywordName, Keyword)
    Cookie.Values.Add(CookieReferringWebsiteName, ReferringWebsite)

    If Not HasCookie Then
      if they dont have the cookie
      System.Web.HttpContext.Current.Response.Cookies.Add(Cookie)
    Else
      if they have the cookie
      System.Web.HttpContext.Current.Response.Cookies.Set(Cookie)    will this overwrite the old one?  I hope...
    End If

  End Sub

And my readcookie function
  Public ReadOnly Property GetCookie() As CookieInfo that datatype at the beginning
    reads the cookies information
    Get
      If Not HasCookie Then
        Exit Property
      Else
        GetCookie.Keyword = CookieKeyword
        GetCookie.SearchEngine = CookieSearchEngine
        GetCookie.ReferringURL = CookieReferringURL
        GetCookie.ReferringWebsite = CookieReferringWebsite
      End If
    End Get
  End Property

  All of the other cookie properties are the same except for the constant in the values clause of the return statement.
  Public ReadOnly Property CookieKeyword() As String
    Get
      If HasCookie Then
        Return System.Web.HttpContext.Current.Request.Cookies(CookieName).Values(CookieKeywordName)
      Else
        Return ""
      End If
    End Get
  End Property

theres a lot more, but thats whats sort of important...

end class

I guess you guys asked for it...that was long.

Hope its not too bad, and I really hope someone can help me with this...

Thanks a lot,
-Starwiz
 

Similar threads

U
Replies
0
Views
152
Unable to read modified values from Azure PATCH
U
E
Replies
0
Views
128
elfenliedtopfan55
E
Back
Top