[Recommended] Develop high-performance ASP.NET applications (derived from .NET Framework SDK documentation)

xiaoxiao2021-03-06  46

The following guidelines list specific technologies, you can use these techniques to ensure that the selected code reaches an acceptable level of performance.

Disable it when you do not use a session state. Not all applications or pages need to be targeted for specific users, and you should disable session status for any applications or page that does not require a session state. To disable the session state of the page, please

@Page

The EnableSessionState property in the instruction is set to false. For example, <% @ page enablessionState = "false"%>.

Note If the page needs to access the conversation variable, but not intended to create or modify them,

@ Page Directive

EnableSessionState property is set to

Readonly.

The session status of the XML Web Services method can also be disabled. For more information, see XML Web Services created using ASP.NET and XML Web Services client. To disable the application's session state, set the MODE property to OFF in the sessionState configuration section of the application web.config file. For example, . Select the session state provider carefully. ASP.NET provides three different ways for the storage application's session data: the session status in the process, as a process of Windows services, and the processes in the SQL Server database. Each method has its own advantages, but the process of session in the process is the fastest speed to date. If you only store less volatile data in a session state, it is recommended that you use the process in the process. Process external solutions are mainly used to zoom applications across multiple processors or multiple computers, or if the data cannot be lost when the server or process is restarted. For more information, see ASP.NET Status Management. Avoid unnecessary round-trip processes of the server. Although you are likely to want to use the Web Forms Frames to save time and code, it is not appropriate to use ASP.NET server controls and rapid restructuring in some cases. Typically, you only need to boot to the server's round-trip process when retrieving or storeing data. Most data operations can be performed on the clients between these round-trip processes. For example, verifying user inputs from an HTML form often proceeds before the data is submitted to the server. Typically, if you don't need to pass the information to the server to store it in the database, then you should not write code that causes the round-trip process. If you develop a custom server control, consider rendering client code for your browser that supports Ecmascript. By using server controls in this way, you can significantly reduce the number of information that is unnecessary to send to the web server. For more information, see Developing an ASP.NET Server Control. use

Page.ispostback

Avoid performing unnecessary processing on the round-trip process. If you write the code for processing the server control return process, you may need to perform other code when you first request a page instead of the code that is executed when the user is included in the HTML form included in this page. Depending on whether the page is generated by the response server control event, use the page.ispostback attribute to execute the code. For example, the following code demonstrates how to create a database connection and command, which is bound to the data when the page is requested.

DataGrid

Server controls. [Visual Basic]

SUB Page_Load (Sender As Object, E as Eventargs)

'Set up a connection and command here.

IF not (page.ispostback)

DIM Query As String = "SELECT * from authors where firstname like '% Justin%'" MyCommand.Fill (DS, "Authors")

MyDataGrid.Database

END IF

End Sub [C #]

Void Page_Load (Object Sender, Eventargs E) {

// set up a connection and common here.

IF (! page.ispostback) {

String query = "select * from authors where firstname Like '% Justin%'"

MyCommand.Fill (DS, "Authors");

MyDataGrid.databind ();

}

} Due to each request, the Page_Load event is executed, the above code checks if the iSPostBack property is set to false. If yes, execute the code. If this property is set to True, the code is not executed.

Note If this check is not running, the behavior of the back page will not change.

The code for the Page_Load event is executed before executing the server control event, but only the result of the server control event may be rendered on the output page. If you don't run the check, you will still be

Page_Load event and any server control event on this page performs processing.

Use the ASP.NET server control in an appropriate environment. Check your application code to make sure it is necessary to use the ASP.NET server control. Even if they are very easy to use, server controls don't always complete the best choice for tasks because they use server resources. In many cases, a simple rendering or data binding will complete the task. The following example demonstrates that using the server control is not the most efficient way to send values ​​to the HTML sent to the client. Each method sends a path to the image that is displayed by the request browser, but is not the most advantageous method using server control, because the Page_Load event requires calling the server for processing. Instead, use the presentation statement or data binding expression. [Visual Basic]