WWW Codeproject Com Articles 13100 ASP NET Best Practices Fo
WWW Codeproject Com Articles 13100 ASP NET Best Practices Fo
12,233,499
Developer
members (51,191
Newsletter.
online)
home
articles
quick answers
Sign in
discussions
features
community
help
Rate this:
This article lists the techniques that you can use to maximize the performance of your ASP.NET applications. It provides common issues,
design guidelines, and coding tips to build optimal and robust solutions.
Introduction
ASP.NET is much more powerful than classic ASP, however it is important to understand how to use that power to build highly efficient,
reliable and robust applications. In this article, I tried to highlight the key tips you can use to maximize the performance of your ASP.NET
pages. The list can be much longer, I am only emphasizing the most important ones.
2. String concatenation
If not handled properly, String Concatenation can really decrease the performance of your application. You can concatenate strings in two
ways.
First, by using string and adding the new string to an existing string. However, this operation is really expensive (especially if you
are concatenating the string within a loop). When you add a string to an existing string, the Framework copies both the existing and
new data to the memory, deletes the existing string, and reads data in a new string. This operation can be very time consuming and
costly in lengthy string concatenation operations.
The second and better way to concatenate strings is using the StringBuilder Class. Below is an example of both approaches.
If you are considering doing any type of String Concatenation, please do yourself a favor and test both routines separately. You
may be surprised at the results.
Hide Copy Code
pdfcrowd.com
str += i.ToString()
Next i
Dim EndTime As DateTime = DateTime.Now
Response.Write(("<br>End time:" + EndTime.ToString()))
Response.Write(("<br># of time Concatenated: " + i.ToString))
String Class
Start time: 2/15/2006 10:21:24 AM
End time: 2/15/2006 10:25:47 AM
# of time Concatenated: 100000
Hide Copy Code
StringBuilder Class
Start time: 2/15/2006 10:31:22 AM
Stop time:2/15/2006 10:31:22 AM
# of time Concatenated: 100000
This is one of the many situations in which ASP.NET provides extremely high performance benefits over classic ASP.
In some situations performing postback event handling are unnecessary. You can use client callbacks to read data from the server
instead of performing a full round trip. Click here for details.
pdfcrowd.com
ViewState is turned on in ASP.NET by default. You might not need ViewState because your page is output-only or because you
explicitly reload data for each request. You do not need ViewState in the following situations:
Your page does not post back. If the page does not post information back to itself, if the page is only used for output, and if the
page does not rely on response processing, you do not need ViewState.
You do not handle server control events. If your server controls do not handle events, and if your server controls have no dynamic
or data bound property values, or they are set in code on every request, you do not need ViewState.
You repopulate controls with every page refresh. If you ignore old data, and if you repopulate the server control each time the page
is refreshed, you do not need ViewState.
Disabling viewstate
There are several ways to disable ViewState at various levels:
To disable ViewState for a single control on a page, set the EnableViewState property of the control to false.
To disable ViewState for a single page, set the EnableViewState attribute in the @ Page directive to false. i.e.
Hide Copy Code
To disable ViewState for a specific application, use the following element in the Web.config file of the application:
Hide Copy Code
To disable ViewState for all applications on a Web server, configure the <pages> element in the Machine.config file as follows:
Hide Copy Code
If a page requires access to session variables but will not create or modify them, set the EnableSessionState attribute in the@
Page directive to ReadOnly. i.e.
Hide Copy Code
To disable session state for a specific application, use the following element in the Web.config file of the application.
Hide Copy Code
<sessionState mode='Off'/>
To disable session state for all applications on your Web server, use the following element in the Machine.config file:
Hide Copy Code
pdfcrowd.com
<sessionState mode='Off'/>
6. Use Server.Transfer
Use the Server.Transfer method to redirect between pages in the same application. Using this method in a page, with
Server.Transfer syntax, avoids unnecessary client-side redirection. Consider Using Server.Transfer Instead of
Response.Redirect. However, you cannot always just replace Response.Redirect calls with Server.Transfer. If you need
authentication and authorization checks during redirection, use Response.Redirect instead of Server.Transfer because the
two mechanisms are not equivalent. When you use Response.Redirect, ensure you use the overloaded method that accepts a
Boolean second parameter, and pass a value of false to ensure an internal exception is not raised. Also note that you can only
use Server.Transfer to transfer control to pages in the same application. To transfer to pages in other applications, you must use
Response.Redirect.
pdfcrowd.com
Check for null values. If it is possible for an object to be null, check to make sure it is not null, rather then throwing an
exception. This commonly occurs when you retrieve items from ViewState, session state, application state, or cache objects as well
as query string and form field variables. For example, do not use the following code to access session state information.
Hide Copy Code
Try
_con.Open()
pdfcrowd.com
Catch ex As Exception
Throw ex
Finally
If Not _con Is Nothing Then
_con.Close()
End If
End Try
<configuration>
<system.web>
<trace enabled="false" pageOutput="false" />
<compilation debug="false" />
</system.web>
</configuration>
References
Improving .NET Application Performance and Scalability (Retrieved 2006, February 12).
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt
please contact the author via the discussion board below.
A list of licenses authors might use can be found here
Share
open in browser PRO version
pdfcrowd.com
Spacing Relaxed
Go
Per page 50
Update
pdfcrowd.com
My vote of 4
amnk.info
16-May-13 21:02
My vote of 5
M.Shawamreh
2-Apr-13 23:06
My vote of 5
HariPrasad katakam
21-Feb-13 0:12
My vote of 4
Eduardo Ceratti
27-Nov-12 7:47
My vote of 5
31-Oct-12 7:29
My vote of 5
rajyarr
17-Dec-11 16:05
well done for your article, It helped us alot, Thank you very
much!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
AWERT1232003
11-Mar-08 3:19
rickstrand
21-Feb-08 13:29
Exception Handling
Chris L
9-Dec-07 6:24
Kikoz68 + BlackBerry
Chipalino
27-Nov-07 5:36
AMassani
24-Nov-07 22:55
dsmportal
22-Nov-07 4:05
Well...
Kikoz68
19-Nov-07 19:30
10
Translate in Portuguese
18-Nov-07 3:47
Leshia
21-May-07 4:34
rama charan
12-Feb-07 4:28
rama charan
12-Feb-07 4:18
DanInManchester
3-Jul-06 5:44
benoityip
16-Mar-06 18:54
15-Mar-06 12:07
More resources
tvidigal
8-Mar-06 7:33
Postback's
DarrenKopp
2-Mar-06 14:46
Correction
DarrenKopp
2-Mar-06 14:21
String concatenation?
Masjj
2-Mar-06 11:26
Inaccuracies
Shazam999
2-Mar-06 8:48
14
Stringbuilder
IPC2000
2-Mar-06 5:25
Simon Capewell
22-Feb-06 0:19
Dave2345
19-Feb-06 20:20
Well done
Jason. B
19-Feb-06 5:14
pdfcrowd.com
Sting concatenation
News
17-Feb-06 18:12
tkdmaster
Question
Refresh
Bug
Answer
Select Language
Joke
Praise
Rant
Admin
Article Copyright 2006 by Ali Khan (OKC)
Everything else Copyright CodeProject, 1999-2016
pdfcrowd.com