Some common performance optimization methods in ASP. NET
Source: Internet
Author: User
The cache mechanism of ASP. NET is greatly improved compared with ASP. NET. This document not only summarizes and introduces common optimization methods, but also emphasizes how to use the cache of ASP. NET to obtain the best performance.
1: Do not use unnecessary sessions
Like ASP, do not use session when it is not necessary.
For the entire application Program Or disable the session status on the page:
L disable the session Status of the page
L disable the session Status of the application
In the sessionstate configuration section of the web. config file of the application, set the mode attribute to off.
That is :.
2: Unnecessary Server Control
In Asp.net, a large number of server-side controls facilitate program development, but may also lead to performance loss, because each time a user operates a server-side control, a round-trip process with the server is generated. Therefore, server control should be used less unless necessary.
3: Unnecessary viewstate
By default, ASP. NET enables viewstate (view State) for all server control ). However, viewstate needs to save some information on the client, which may cause performance consumption. If you must use server control, you can disable viewstate.
There are two ways to disable viewstate: Disable viewstate for the entire page or a single control.
L for controls
L for pages
4: Do not use exception to control program processes
Some programmers may use exceptions to implement some process control. For example:
Try {
Result = 100/num;
}
Catch (exception E)
{
Result = 0;
}
However, exceptions consume a lot of system performance. Unless necessary, exception control should not be used to implement the program process.
The above Code It should be written as follows:
If (num! = 0)
Result = 100/num;
Else
Result = 0;
5. Disable VB and JScript dynamic data types.
Should the pen Jia fault allows the mirror Yan Yang Xi thirst Xi Huang call Xiao Ben Xiao n playing tip planting Xiao?
6. Access Data Using Stored Procedures
7. Do not use dataset for read-only data access.
As a powerful and offline database, dataset has a relatively high performance cost. Other datasets in. Net can be used as replacements in specific scenarios.
N use sqldatareader instead of dataset;
N sqldatareader is read-only, forward-only.
8: Disable ASP. NET debug mode
To facilitate development and debugging, the debug mode is enabled by default in vs.net. When deploying an application, disable the debug mode, which effectively improves the application performance.
9: use ASP. net output cache to buffer data;
The buffer function is a very powerful function in Asp.net. I have seen some evaluations that the performance of the Asp.net program is several times faster than that of Sun's JSP application. In fact, a very important aspect of this evaluation program is the use of many Asp.net buffer functions.
Common buffer methods in Asp.net include:
N page Buffering
Example: Query weather conditions in Beijing. Because the weather data is relatively specified within a certain period of time.
When the web program first queries weather in Beijing, the application may call a remote WebService to obtain weather information. Then, users can obtain the current weather information from the buffer. This will greatly improve the performance and reduce the pressure on the server.
Method:
U: indicates that the page is buffered.
U Duration: controls the buffer effective time, in minutes.
U varybyparam: used to determine whether to buffer data. For example, if the first user queries the weather in Beijing, the weather in Beijing is stored in the buffer. When the second user queries the weather in Shanghai, the following code can be used to buffer the weather in multiple cities to avoid reading errors:
This indicates that multiple data copies are buffered according to the cityname parameter in the page url.
N-part Buffer
In Asp.net, in addition to page buffering, the output cache parameter can also be used for user control to buffer user controls. Similarly, controls of the same type in a page can have multiple different buffers. Different buffering methods can be implemented based on parameters.
For example, the control can implement different buffering Based on the C attribute of control.
N Data Buffer
N buffer expiration dependency Conditions
In a sense, cache and application are the same and are all common objects. In order to strike a balance between buffering and Data Validity, you can set a buffer expiration Policy as needed.
U file dependency
Cache. insert ("mydata", Source
, New cachedependency (server. mappath ("authors. xml ")))
The meaning of this Code is that when the authors. xml file does not change, the buffer mydata is always valid.
U time dependency
Set the expiration time to 1 hour. This is an absolute expiration time.
Cache. insert ("mydata", source, null
, Datetime. Now. addhours (1), timespan. Zero );
U relative expiration dependency
The buffer expires 20 minutes after the dataset does not change.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service