Click here to Skip to main content
15,884,237 members
Articles / Web Development / ASP.NET
Tip/Trick

Post Serialized Objects (Data) in ASP.NET

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Feb 2013CPOL2 min read 23K   5   8
This is helper class to post serialized objects (Data) to other page in asp.net.

Introduction 

This is helper class to post serialized objects to other page.

Background

Many times we need to carry data from one page to another page in ASP.NET. Out of available options query string suffers security issues and size limitations; Server.Transfer is secure but keeps same old URL in browser. Third approach is posting data to page. But ASP.NET does not provide any built any functionality to do that. Here is a helper class which serializes objects, encrypts them and then posts to destination URL. Same utility can be used at destination page to read the data.

How it works?

Whole process includes following steps.

1. Serializing data :

To post data, it first needs to be serialized into a string. Here object is serialized using LosFormatter (used to serialize viewstate). Other options that can be used are XMLSerializer and SoapFormatter or any other formatter that converts objects to strings. XmlSerializer suffers from a drawback that it cannot serialize objects which implement IDictionary interface. While choosing a formatter, you will be required to think about time taken for serializing and de-serializing objects and size of formatted data.

2. Encrypting data: 

As data will be written to client before it gets posted, encryption is necessary to ensure security. Here Triple DES encryption is used. Other encryption techniques can also be used.

3. Posting data

Serialized, encrypted and HTML encoded objects are then written to client in a multipart form, as hidden fields, which is then auto-submitted using JavaScript. Noscript tag must also be written, as JavaScript might be disabled and in such a case application may get stuck and user will not know what actually happened. I have intentionally added one variable with key __TransferData, to distinguish between data posted by other utilities. It also carries Form Name as value.

4. Reading back

Reading involves decoding, decrypting and de-serializing objects.    

Using the code

Following lines of code illustrate how this utility class can be used to post data to other page:

C#
PostDataHelper helper = new PostDataHelper(Request, Response);

//Set Form Name, by which receiving page may identify posting page
helper.FormName = "Person Form";

//Add objects with unique keys
helper.Data.Add("Person1", new Person("ABC", "XYZ"));
helper.Data.Add("Person2", new Person("123", "456"));

//Call method to post data
helper.RedirectWithData("Default2.aspx");

Objects being added need to be serializable.  As MSDN state here, Losformatter is for classes containing strings, arrays, and hash tables. So if efficiency is a concern, you can check for other serialization options. 

Reading the data is equally simple:

C#
PostDataHelper helper = new PostDataHelper(Request, Response);

//Read posted data
helper.ReadPostedData();

//Read form name and objects
Response.Write("Form Name : " + helper.FormName + "<br/>");
if (helper.Data.HasObjectForKey("Person1"))
    Response.Write("Person1 : " + helper.Data.Get("Person1") + "<br/>");

if (helper.Data.HasObjectForKey("Person2"))
    Response.Write("Person2 : " + helper.Data.Get("Person2") + "<br/>");

Form name can be used in cases where multiple pages are posting to same page and you need to differentiate between them.

Note

If page, which is posting data, is intended to be called through a WebRequest, this utility class won't work. Reason behind this is use of JavaScript for form submission, which won't be executed by WebRequest. If, only WebRequest is going to be used (like API), you can opt for Server.Transfer and in case of mixed mode i.e. both WebRequest and browser, a flag can be passed to differentiate between calls.

History 

  • Feb 25 2013: Update content and added note.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer TCS
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionReally cool concept Pin
Member 300191027-Feb-14 15:10
Member 300191027-Feb-14 15:10 
AnswerRe: Really cool concept Pin
ajaynarewade27-Feb-14 22:12
ajaynarewade27-Feb-14 22:12 
SuggestionWhat about using a Session variable? Pin
Andrew Rissing25-Feb-13 4:26
Andrew Rissing25-Feb-13 4:26 
GeneralRe: What about using a Session variable? Pin
ajaynarewade25-Feb-13 4:43
ajaynarewade25-Feb-13 4:43 
QuestionNice Article Pin
Anil Srivastava24-Feb-13 5:02
Anil Srivastava24-Feb-13 5:02 
AnswerRe: Nice Article Pin
ajaynarewade24-Feb-13 14:17
ajaynarewade24-Feb-13 14:17 
GeneralThat's nice! Pin
daniele.arrighi24-Feb-13 1:27
daniele.arrighi24-Feb-13 1:27 
GeneralRe: That's nice! Pin
ajaynarewade24-Feb-13 1:48
ajaynarewade24-Feb-13 1:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.