Some methods of optimizing performance in asp.net

Source: Internet
Author: User
Asp.net| Performance | Optimization of some of the best performance methods used in asp.net


ASP.net's caching mechanism is greatly improved compared with ASP, this document, in addition to summarizing the common optimization methods, emphasizes how to use the ASP.net cache for optimal performance.

1: Do not use unnecessary sessions
As in ASP, do not use the session when it is not necessary.

Session state can be disabled for an entire application or page:

L Disable session state for a page


• Disabling session state for an application

In the sessionstate configuration section of the application's Web.config file, set the Mode property to OFF.

namely:.



2: Do not use unnecessary server control
In ASP.net, a large number of server-side controls facilitate program development, but can also result in a loss of performance because each time a user operates a server-side control, a roundtrip process with the server side is generated. Therefore, it is not necessary to use server control sparingly.



3: Do not use unnecessary ViewState
By default, ASP. NET has ViewState (view state) enabled for all server control. But viewstate needs to keep some information on the client, which can result in a performance drain. You can consider prohibiting viewstate when you must use the server control.

There are two ways to prevent ViewState: Disable viewstate for an entire page or for a single control.

L for controls


L for Pages


4: Do not use exception control program flow
Some programmers may use exceptions to implement some process control. For example:



try{

Result=100/num;

}

Catch (Exception e)

{

result=0;

}

But in fact, exception is very consuming system performance. Unless necessary, exception control should not be used to implement program flow.

The above code should be written as:



if (num!=0)

Result=100/num;

Else

result=0;

5: Disable VB and JScript Dynamic Data types
Variable data types should always be shown, which saves the execution time of the program. To do this, you can specify in front of the page:

6: Use stored procedures to complete data access
7: Read-only data access do not use a dataset.
The dataset is a powerful, offline-enabled database with a relatively high performance cost. Can be used on a particular occasion. NET as a substitute for other datasets in.

n Use SqlDataReader instead of dataset;

N SqlDataReader is read-only,forward-only.

8: Turn off the asp.net debug mode
For easy development and debugging, the debug mode is turned on by default in Vs.net, and the debug mode should be turned off when the application is deployed, which will improve application performance effectively.

9: Using asp.net Output cache buffer data;
Providing buffering is a very powerful function in asp.net. There have been some reviews that said: The performance of the ASP.net program is several times faster than that of Sun's JSP application, and in fact, the most important point of the evaluation program is the use of many asp.net buffering functions.

The commonly used buffering methods in asp.net are:

N Page Buffering

An example: Check the weather in Beijing. Because the weather data is relative to the rule in a certain time.

When you first query the weather in Beijing in a Web program, your application may be calling a remote webservice to get weather information. And then the user can get the current weather information from the buffer. This will greatly improve performance and reduce server pressure.

Way:

U: Indicates that the page uses a buffer

U Duration: Controls the buffer to be valid time, the unit is minute.

U VaryByParam: A judgment basis for indicating whether or not to buffer. For example, if the first user inquires about the weather in Beijing, the weather is stored in the buffer. When a second user queries the weather in Shanghai, to avoid reading to the wrong buffer, you can use such code to buffer the weather in multiple cities:


This indicates that multiple copies of data are buffered according to the CityName parameter in the page URL.

N Fragment Buffering

In ASP.net, in addition to using buffering in the page range, you can also use the output cache parameter for user control to buffer your users ' controls. Similarly, a control of the same type on a page can have several different buffers. Different buffers can be implemented according to the parameters.

For example, you can implement a different buffer for a control based on the different C properties of controls.



N Data buffering



N-Buffered expiration dependency condition

In a sense, the cache and the application are the same, are a public object. To achieve a balance between buffering and data availability, the buffer expiration policy can be set appropriately as needed.

U file dependencies

Cache.Insert ("Mydata", Source

, New CacheDependency (Server.MapPath ("Authors.xml"))

The meaning of this code is that the buffer MyData is always valid when the Authors.xml file does not change.



U time Dependent

Set to expire after 1 hours, which is an absolute expiration.

Cache.Insert ("Mydata", Source,null

, DateTime.Now.AddHours (1), TimeSpan.Zero);



U relative expiration dependent

The buffer expires after the dataset is no longer changed for 20 minutes.

Cache.Insert ("MyData", Source,null

, Datetime.maxvalue,timespan.fromminutes (20));




Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.