|
| |
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
Re-look at techniques of passing data between pages
|
|
by
Jignesh Desai
|
|
Published on: Saturday, 12-Jan-2008
|
Comments Received: 0
|
|
|
|
A Re-look at passing data techni
A Re-look at techniques of passing data between pages.
In all of my training session a common question that I faced
is that what is the best way to pass data between pages, and mostly the question
is asked immediately after I finish my 3 hours of talk and explained everything
on "Session Management" in ASP.NET. ahhh!! A tingling effect passes through my
entire body as if someone splash cold water on my face. Sounds like after
listening to entire "Ramayana" some one asks Who's Shree Ram? ... LOL.....So I
thought let me write something which can act as a guidance. Thow you will find
many many articles on the subject but I indent my visitors use this as "Quick
HandBook"
So Question1: How many ways are there to pass data between
pages.
1. HTTP Get - The QueryStrings: One of the first and oldest techniques still
used. On the end of the URL address you add "?" symbol and pass key=value pair
each separated by "&" symbol. Its like as you pass parameters to a function,
here you pass parameters to a URL. What ever you pass is visible to the user in
the address bar of their browser .
Plus and Minus:
-
User can bookmark the URL
-
You can customize page output based on parameters passed
-
Yon can pass 'n' no of parameters
-
You can secure it if your data is sensitive using some scrambling techniques
-
You must also keep your query string within Internet Explorer's 2,083
characters limit.
-
I am not sure which, but some browsers do not support blank spaces eg.
http://www.dotnetjini.com?name=Jignesh Desai. you will need to use
Server.URLEncode to encode all the characters within the URL as a best practice,
after which URL should look like http://www.dotnetjini.com?name=Jignesh%20Desai.
2. HTTP Post technique: With size limit of querystring POST is the preferred
method for sending lengthy form data. when a form is submitted user does not see
the form data that was sent to calling page.
Plus and Minus:
-
You can post virtually unlimited data
-
Binary data(Uploading of file) can be passed using POST technique only.
-
Data is secure as its not visible to users as in QueryString technique.
-
If you wish to send any additional information you can use HTML <Hidden>
field to pass it along with other data.
-
In contrast to HTTP GET technique POST request sends additional data to the
web server in HTTP Headers
3. Cookies: Allows you to store bit of information not more then 4096 bytes on
users computer. Browser does read/write of cookies on users computer. Cookies
are temporary in nature unless you specify expiry datetime after which it
expires automatically (Client-side persistence cookies). They are often used by
browsers to maintain a Session ID (SID). [More Info...]
Plus and Minus:
-
Smart users can tamper data stored in cookies so avoid storing sensitive
data.
-
Remember each time you access server browser will sent relevant cookies.
-
Can be used to store username and password so that next time user can AutoLogin, so called "Remember Me" feature. Ensure you encrypt such data rather
then storing as simple text.
-
Specifying the domain name to indicate the end of the domain for which the
cookie is valid.
-
Use Cookies only when your website should execute using specific setting from
that particular computer.
4. Database: Good thinking if you want to audit information for each user.
Normally used to understand behavioral pattern of users & visitors. You will
need to periodically delete the data after you have abstracted information you
need.
Plus and Minus:
-
Too much of database operation will impact sites performance.
5. Application / Cache: Used to store global data accessible to all users.
Object "Application" is there from ASP days, where as a similar object "Cache"
with add capabilities was introduced in ASP.NET.
Plus and Minus:
-
Data is stored on server side.
-
Its important to plan what data is needed to store since it occupying
server's memory.
-
.NET out-of-box offers you to choose to store these data either in server's
memory(Default) or A dedicated machine or A SQL Server Database, all done by a
simple XML based configuration(Web.Config), which means you can change your
storage technique on fly.
-
Choose Application or Cache when you are sure that data stored will not be
updated so frequently.
-
Must call lock() and unlock() before modifying Application object. This is
not need for Cache objects.
-
Cache object is similar to application but has added features like expiry
policy(Sliding Expiration, Absolute Expiration, dependency based Expiration,
dependency can be a file, other similar objects or SQLServer Table)
-
Can be used to securely store "ConnectionString" instead of Web.config file
where it is stored as plain text.
-
Another example can be Shopping cart data which is same for all users and
likely to change once in a month or week.
-
Since Cache object can expire automatically always check if CacheObject is
not null before accessing values stored in it.
-
Lookout for global.asax for Application Start and End events, Ideal place to
declare and initiate objects.
-
Be Sure if you are storing objects, they are serializable.
-
Cache object uses some intelligent techniques for memory management, objects
are automatically flush out of memory if it starts to get low or object not
frequently used. Change Cache.EffectivePercentagePhysicalMemoryLimit
to specify
percentage of physical memory that can be consumed by an application before
ASP.NET starts removing items from the cache.
-
Before you add more items into Cache, you can check for Cache.EffectivePrivateBytesLimit property which returns the number of bytes that
can be used by the application process.
6. Session: Session objects are used to store user specific data. Every user
session gets a unique session ID generated by Web Server. Server creates a new
Session object for each new user, and destroys the Session object when the
session expires.
Plus and Minus:
-
Like Application and Cache Its important to plan what data is needed to store
in Session objects since it occupying server's memory.
-
Its also important to plan because Session objects can consume up resources
faster as no of visitors increases.
-
.NET out-of-box offers you to choose to store these data either in server's
memory(Default) or A dedicated machine or A SQL Server Database, all done by a
simple XML based configuration(Web.Config), which means you can change your
storage technique on fly.
-
Be Sure if you are storing objects, they are serializable
-
Lookout for global.asax for Session Start and End events, Ideal place to
declare and initiate objects.
-
SessionID is stored in cookies which is passed back to server on each
request. if browser has disabled cookies due to security reasons you can still
manage sessions by altering web.config setting eg <sessionState cookieless="true"></sessionState>
-
Configure Session.Timeout as per your need, default is "20" minutes. which is
ideal in most scenarios.
7. ViewStates: The ViewState object is useful for storing and remembering data
between postbacks to the same page. It cannot be used for passing values to
other pages.
Plus and Minus:
-
Can be used to remember sorting order between postbacks
-
ViewState does not user server resources as data is stored within the page
itself under a hidden variable "__VIEWSTATE" and passed to server and back on
each request until the user is on the same .aspx page.
-
Not advised to store sensitive data as its not encrypted.
-
Not advised to store large amount of data as its going to consume internet
bandwidth impacting page performance.
-
Control ViewState by setting it to "off" for either control or page when not
required.
8. HttpContext Object: Lesser known but HttpContext is associated with the
current request. The Items property on this object is a collection that you can
use to pass things around for the currently executing request *only*. Can say
its similar to POST but in a secure way. eg.
Context.Items("DiscountChoice") = txtDiscountChoice.Text 'On Page1
Server.Transfer("CalculateBill.aspx") 'On Page1
Dim s As String = Context.Items("DiscountChoice").ToString 'On Page2
The static property Current of the HttpContext class can be useful whenever you
want to grab the HTTP object from your custom class eg.
Class MyClass
Public Sub MySubRoutine
HttpContext.Current.Response.Write("I can output HTML from my Custom Class
Also.");
End Sub
End Class
Summary
| Technique |
Object Scope |
Server Memory
Utilization |
Bandwidth
Utilization |
Secure |
Score |
| Http-Get - The QueryStrings |
Page Level(Between 1 or 2 Pages) |
Low |
Medium |
No, Unless Encrypted. |
3 |
| Http-POST |
Page Level(Between 1 or 2 Pages) |
Low |
Low/Medium |
Low |
4 |
| Cookies |
User Level |
N/A |
Low |
Low, Unless Encrypted |
5 |
| DataBase |
Custom |
Medium/High |
Medium |
High |
8 |
| Application / Cache |
Application Level(Shared between Users) |
Medium |
Low |
High |
2 |
| Session |
User Level |
Medium/High |
Low |
High |
1 |
| ViewState |
Page Level (Single Page, Postbacks only) |
Low |
High |
Medium |
6 |
| HttpContext |
Page Level (Between 2 Pages for current request) |
Low |
Low/Medium |
Medium |
7 |
|
|
|
|
|
|
|
|
|