Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Objects via QueryString

Tags:

asp.net

vb.net

I have object A which in turn has a property of type Object B

Class A

property x as Object B

End Class

On my ASP.NET page when I select a gridview item which maps to an object of type A I serialize the object onto the QueryString and pass it to the next page.

However I run into problems if property x actually has some value as it looks like I exceed the QueryString capacity length of 4k (although I didn't think the objects were that large)

I have already considered the following approaches to do this

  • Session Variables

Approach not used as I have read that this is bad practice.

  • Using a unique key for the object and retrieving it on the next page.

Approach not used as the objects do not map to a single instance in a table, they arte composed of data from different databases.

So I guess my question is two fold

  • Is it worth using GKZip to compress the querystring further (is this possible??)
  • What other methods would people suggest to do this?
like image 527
Dean Avatar asked Dec 13 '22 06:12

Dean


1 Answers

If displaying the url of the next page in the browser does not matter, you could use the context.items collection.

context.items.add("keyA", objectA)
server.transfer("nextPage.aspx")

Then on the next page:

public sub page_load(...)
    dim objectA as A = ctype(context.items("keyA"), objectA)
    dim objectB as B = objectA.B
end sub

One reason to use this is if you want the users to believe that the next page is really a part of the first page. To them, it only appears as if a PostBack has occurred.

Also, you don't really need a unique key using this approach if the only way to use "next page" is if you first came from "first page". The scope for the context items collections is specific to just this particular request.

I agree with the other posters who mentioned that serialized objects on the querystring is a much worse evil than using session state. If you do use session state, just remember to clear the key you use immediately after using it.

like image 178
Chad Braun-Duin Avatar answered Feb 17 '23 07:02

Chad Braun-Duin