Disable Session State
Disable Session state for sites that do not make use of it. The performance improvement
may be very minimum, but cutting down extra processing for every request is always
good. Session state can be disabled in the web.config file:
<configuration>
<system.web>
<sessionState mode="Off" />
</system.web>
</configuration>
Session state can also be disabled on individual pages by modifying the "EnableSessionState"
page directive on each page.
Use Output Buffering
By default its enabled. ASP.Net sends response to IIS in a 31 KB buffer, which is
then passed to the client. When buffering is disabled, ASP.NET only sends few characters
to IIS, thus not utilizing this buffer. Which in turn increases the trips between
IIS and the worker process.
To enable it you can change the web.config or you can enable it on each page through
@page directive
<pages buffer="true"></pages>
Or
<% @Page Buffer="true" %>
Avoid Server-Side Validation
Whereever you can use client-side validation instead of Server-Side validation.
This will save you from additional reposts to the server in cases of invalid input.
Use Repeater Control And Avoid DataList, DataGrid, and DataView controls
Take advantage of HttpResponse.IsClientConnected, before performing a large operation
The IsClientConnected property returns True if the client is still connected to
the Web server during the processing of a response.
Use HTTPServerUtility.Transfer instead of Response.Redirect
Instead of using Response.Redirect, use HTTPServerUtility.Transfer where ever you
can. Response.Redirect sends response to the client which then sends a new request
to the server. HTTPServerUtility.Transfer however performs the redirect on the server.
Only use Response.Redirect when you want authentication and authorization to be
performed on redirects or you want URL on client browser to be changed because HTTPServerUtility.Transfer
will not do this as it is a server side transfer.
Always check Page.IsValid when using Validator Controls
If you don’t trust the browsers that they will be able to perform complex validations
you can still use client-side validation and on repost check Page.IsValid to check
if the input passed the given set of rules.
Deploy with Release Build
Win32 Debug :
Full symbolic debugging information is stored in Microsoft format
No optimization
Win32 Release :
No symbolic debugging information is stored in Microsoft format
Optimized for maximum speed
Turn off Tracing
It is important to turn off tracing before deploying application to production.
Tracing adds additional overload to your application which is not required in production
environment. You can disable tracing in the web.config:
<configuration>
<system.web>
<trace enabled="false" />
</system.web>
</configuration>
Use Page.IsPostBack
Avoid Exceptions
Managing exception can make development work very easy if used efficiently. However,
inappropriate handling of exceptions can degrade the performance of your ASP.Net
application.
Use Caching
Caching in ASP.NET dramatically help in boosting application performance by reducing
the load on the underlying server by serving cached content that doesn’t need to
be recreated on each request. ASP.NET provides two types of caching:
Output Cache: -
which stores dynamic pages and user controls. One each request code is not executed
if a cached version of page or control is available
Data Cache: -
which allows application to save application objects, DataSet etc in server memory
so they are not recreated on each request. Use caching whenever possible to reduce
the load on your web server and to increase response time.
Create Per-Request Cache
In ASP.NET, per-request caching can be done by making use of HttpContext.Items property
for data storage. HttpContext is a class that ASP.NET uses for the life cycle of
a request/response. For example, when you make a call to Response.Write, you're
actually calling HttpContext.Current.Response.Write.
Use of StringBuilder Class for String Manipulation
Turn Off ViewState for controls where Viewstate is not necessary
Avoid using viewstate for storing large objects.Disable it when you don’t need it.
ViewState is used by the ASP.Net server controls so that they can retain their state
after postback. ASP.NET serializes all objects,controls in the viewstate and transmits
them in a hidden field to the client browser. If it is not managed properly, viewstate
can increase page size which in result increases network traffic. Also precious
CPU cycles are used for serialization, deserialization of viewstate objects.You
can disable viewstate if:
- Your pages don’t do postback
- Your controls are not bound to a data source or they don’t handle server events
or their properties are set on each page postback
- You recreate controls on every postback. You can disable viewstate in both web.config
(Site Specific) or @Page directive (Page Specific).
<pages enableViewState="false"></pages>
or
<%@ Page EnableViewState="false"%>
Use Paging
When ever using data grid to show data with paging enabled one thing needs to understood
that if your query returned let say 1000 record and you are only showing 100 records
per page the rest of the 900 record will be discarding and the same will apply when
ever you will change the page or apply sorting. The additional 900 rows will definitely
take up memory and if your database is located on a different server which is most
commonly the case you will also be transferring unnecessary data over the network.
Make sure you are only returning the required results to the ASP.NET application
by filtering out the data in your database query and apply custom paging. SQL Server
2005 and onwards provide valuable function for ranking data that can be used to
accomplish this task.
Use the AppOffline.htm when updating binaries (Applicable Only .Net Framework 2.0
and above)
Use ControlState and not ViewState for Controls
Use the Finally Method
Use Option Strict and Option Explicit