ASP. NET: Performance and cache [reprint]

Source: Internet
Author: User
ASP. NET: Performance and cache: Some network lectures in March were downloaded in the MSDN network class. There are many things that make sense. One of the ASP. NET Series Lectures: Performance and cache is taught by Wang Linan, a Microsoft development tool expert. Mr Wang's explanation is very clear, and the courseware is also very detailed. Although it is a network lecture, the Down video file sound is very clear. I listened carefully and recorded some of the key points.

This section mainly introduces some tips to improve ASP. NET performance. The following are some tips for the lecture:

1. Avoid unnecessary operations
Page_Load and IsPostBack

In ASP. NET, The Page_Load event is triggered every time you click the page, including the controls on the page. For example, the following code:

Void Page_Load (Object sender, EventArgs e ){
// Set up a connection and command here
If (! Page. IsPostBack ){
String query = "select * from Authors where FirstName like '% JUSTIN % '";
MyCommand. Fill (ds, "Authors ");
MyDataGrid. DataBind ();
}
}

Void Button_Click (Object sender, EventArgs e ){
String query = "select * from Authors where FirstName like '% BRAD % '";
MyCommand. Fill (ds, "Authors ");
MyDataGrid. DataBind ();
}

Leave the if statement in the Page_Load event alone. When you click the page, the code in Page_Load is executed, which queries the data table and places the result in the DataGrid. At the same time, we also implement the function of querying data tables and returning results in the Button Click method, but the data tables and conditions to be queried are slightly different. If there is no if statement, when you Click the Button, it executes Page_Load first and then responds to the Click Event of the Button. Obviously, executing Page_Load is unnecessary. Why? Because the HTTP protocol is transient disconnected. Once the data transmission is complete, the HTTP protocol disconnects the browser from the server. When a user makes a new operation, such as clicking the Button on the page, the page will be reconnected again, and the Page_Load event will inevitably be returned at this time.

Therefore, we need to judge the attribute of Page. IsPostBack in Page_Load. This attribute indicates whether the page is being loaded in response to client sending back, or whether the page is being loaded and accessed for the first time. If this is the first load, the value is false; otherwise, the value is true. The Page_Load Code indicates that if the page is loaded for the first time, the code in if is executed; otherwise, the code is ignored. Therefore, when you click a Button, the code in Page_Load will be skipped because the page has been loaded before, which greatly improves ASP. NET performance.

Ii. disable unnecessary Session statuses

ASP. NET uses Session to save user-related information, which is stored on the server end. Sessions are mainly related to customer authorization. If the Session status is provided, the Session status is first queried every time the page is called, which affects the page execution performance. Therefore, if you only provide a normal page and it has nothing to do with customer authorization, you should disable the Session status. Some people may think that the Session Status of the page has a slight impact on the performance, which is almost negligible. However, imagine that the project you Want to do involves a lot of pages, and the time consumed by each Session query is accumulative, and the impact on performance is still considerable. To disable the Session Status, set it in the html code of ASP. NET:

<% @ Page EnableSessionState = "false" %>

Iii. Use Server Control with caution

1. Do not use Server Control if necessary

ASP. NET provides two controls: Server Control and standard HTML Control. The HTML Control only responds to client events. Server Control provides the RunAtServer attribute, which establishes object ing on the Server. Its functions are more powerful than HTML controls, but it will consume a certain amount of performance. Therefore, when designing the page, select the control based on the actual situation. If you only need to respond to client events, you 'd better select the HTML control, which will greatly improve ASP. NET performance.

2. Disable ViewState of Server Control if not necessary

ViewState records the status in ASP. NET like Session and Application. However, ViewState only applies to pages. For pages with the same user, the ViewState of the control is the same. Because the Control may use different pages, Server Control records different data states through ViewState.

However, not all Server controls require ViewState. For example, if a DataGrid control only fills the DataSet in the control and does not operate on the data, ViewState is not required. In ASP. NET, The ViewState of Server Control is set to true by default, so we need to reset the Control:

<Asp: datagrid EnableViewState = "false" runat = "server"/>

To disable ViewState of all Server Control on the page, use:

4. Do not use Exception to control program processes

Capture exceptions are well known for performance loss. Therefore, Exception can be avoided. For example:

<% @ Page EnableViewState = "false" %>

Try {
Result = 100/num;
}
Catch (Exception e ){
Result = 0;
}

The above code will capture the exception where the divisor is 0. In this case, you can use the if statement to control the program flow:

If (num! = 0)
{
Result = 100/num;
}
Else
{
Result = 0;
}
5. Disable VB and JScript Dynamic Data Types

In ASP. NET, to maintain compatibility with ASP, the dynamic data types of VB and JScript are retained. However, dynamic data types cannot be used in C # and VB. NET. Because the dynamic data type occupies a large amount of CPU during running, it will affect the program performance. Therefore, we recommend that you disable the Dynamic Data Types of VB and JScript:

<% @ Page Language = "VB" Strict = "true" %>

6. Other performance improvement skills
1. Access through Stored Procedure data
2. Do not use DataSet for read-only data access, but use SqlDataReader instead of DataSet, because SqlDataReader is read-only, forward-only
3. Disable the Debug mode of ASP. NET.

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.