Improve ASP. NET Web Application performance

Source: Internet
Author: User
Tags finally block website performance

Reprint: http://www.codeceo.com/article/24-ways-improve-aspnet-web.html

In this article, you'll describe some of the ways and tricks to improve the performance of ASP. As we all know, solving performance problems is a tedious task, and when performance issues arise, everyone is blamed on the developer who wrote the code.

The following is the translation

So how is the performance problem solved? Here are the points that the. NET developer needs to check before the application is released.

1.debug="false"

When you create an ASP. NET Web application, the default setting is "true". During development, setting to "true" is much more useful, but it needs to be set to "false" when the application publishes the deployment.

<compilation defaultlanguage= "C #" debug= "false" targetframework= "4.0"/>
2. Turn off tracing (tracking)

Tracing is very scary and you have not forgotten to close it. If it doesn't work, make sure to edit the Web. config and close it. It will consume a lot of your program resources.

<trace enabled= "false" requestlimit= "ten" pageoutput= "false" tracemode= "SortByTime" localonly= "true" >
3. Disable session

If you do not use session tracking, be sure to disable it. You can set the following in each asp:

<%@ page language= "C #" codebehind= "WebForm1.aspx.cs" autoeventwireup= "false" inherits= "Webapplication1.webform1" Enablesessionstate= "false"%>
4. Deploying an app with a release build

When deploying an application to a production environment, be sure to use the release version mode instead of debug mode. If you use a debug template, the request time-out is extremely easy. Deploy it as a release, and you'll see a big boost in speed.

5. Close the pageView State

The View state is mainly used after submission, and it is only useful if the data on the page is submitted to this web. The default is "true". If you don't use form data callbacks, you can turn off View state.

<%@ page enableviewstate= "false"%>
6. Avoid using Response.Redirect

Redirect (redirect) is cumbersome and is used only to jump from the current physical server development to other servers. If you are simply jumping within this server development page, use the Server.Transfer syntax, which will reduce the number of unnecessary client redirects.

7. Applying the StringBuilder class and using the ToString () method

The string class object is immutable, and the re-assignment of a string object is essentially a re-creation of a string object and assigning the new value to the object, and its method of ToString does not significantly improve performance. When working with strings, it is best to use the StringBuilder class, whose. NET namespace is system.text. Instead of creating a new object, the class directly operates on the string using methods such as Append,remove,insert and returns the result of the operation through the ToString method. The definitions and operation statements are as follows

int num;  System.Text.StringBuilder str = new System.Text.StringBuilder (); Creates a string str. Append (Num. ToString ()); Add numeric num Response.Write (str. ToString); Show Operation results
8. Avoid throwing exceptions

An exception causes slow speed and causes the application page to show an exception so that no other action can be made. You can use Try/catch to make the exception that appears in the log file.

9. Recycling resources using the Finally method

If you use a lot of other database connections and access files in app development, be sure to close them when you're done. The finally block is executed at the end of the program, so the code in this area is guaranteed to be executed, and the closing code must be executed in this development method block.

10. Using Client Script validation

Use client-side validation instead of server developer authentication. Server development data validation will consume a lot of resources on your server development, and will send a large number of page data callbacks.

11. Using Page.IsPostBack

Make sure that you do not execute too many callback codes. Use the Page.IsPostBack property to ensure that only the page initialization logic is performed when a page is loaded for the first time, not to respond to a client postback.

12. Using pagination

Most WEB application data is displayed in tabular form. Paging has the efficiency of utilizing application development programs. Try to display a small amount of data each time, which speeds up the page display.

13. Asynchronous calls using Ajax

Use an Ajax method to make an asynchronous call.

14. Delete Unused httpmodules

For httpmodules, we can understand that: Create a generic HttpApplication event hook that can be inserted into any WEB application. Using HttpModule is reusable and does not require specific language application code, only one entry in Web. config is required. In the Web. config file, delete the unused httpmodules.

15. Avoid recursive functions/nested Loops

You need to avoid nesting loops and recursive functions in any programming language to improve performance.

16. Do not use unnecessary Server Control

In ASP. NET, a large number of server-side controls facilitates program development, but can also result in a loss of performance, as users produce a round-trip to the server side each time the server-side control is operated. Therefore, it is not necessary to use Server Control sparingly.

17. When invoking multiple operations, use multithreading

When the problem occurs, the single-threaded measuring modules runs on this issue for a long time. Therefore, you can use multiple threads to increase the responsiveness of your application.

18. Connection and shutdown of the database

Accessing a database resource requires creating a connection, opening a connection, and closing the connection several operations. These processes need to exchange information with the database multiple times to pass authentication, which consumes server resources more often. ASP. NET provides a connection pool (Connection pool) to improve the performance impact of opening and shutting down the database. The system places the user's database connection in the connection pool, takes out when needed, reclaims the connection when it closes, and waits for the next connection request. The size of the connection pool is limited, and if you still need to create a connection when the connection pool reaches the maximum, you will inevitably have a significant impact on performance. Therefore, after establishing a database connection only when the actual need to open the connection, the use is closed immediately, so as to minimize the database connection open time, to avoid the situation beyond the connection limit.

19. Using the SqlDataReader class for fast forward-only data cursors

The SqlDataReader class provides a way to read a forward-only stream of data retrieved from a SQL Server database. The SqlDataReader class provides higher performance than the DataSet class if you are allowed to use it when you create an ASP. This is the case because SqlDataReader uses SQL Server's native network data transfer format to read data directly from the database connection. In addition, the SqlDataReader class implements the IEnumerable interface, which also allows you to bind data to server controls. For more information, see the SqlDataReader class. For information about how ASP. NET accesses data, see Accessing Data through ASP.

20. High-performance SQL statement rules
    • Try to avoid full table scanning
    • Try to avoid null values for a field in the WHERE clause
    • Try to avoid using the! = or <> operator in the WHERE clause
    • Try to avoid using or in the WHERE clause to join the condition
    • In and not in also use caution
    • Do not perform functions, arithmetic operations, or other expression operations on the left side of "=" in the WHERE clause
    • UPDATE statement, do not update all fields if only 1 or 2 fields are changed
    • For multiple large data volume (here hundreds of even larger) table join, to first paged and then join, otherwise the logical reading will be very high, poor performance
    • Use Varchar/nvarchar as much as possible instead of Char/nchar for more rules, refer to: http://database.51cto.com/art/201407/445934.htm
21. Caching

Cache is a space for time, the popular point is to put the data you get in memory for a period of time, in this short time the server does not read the database, or the real data source, but read the data you stored in memory. Caching is an indispensable data processing mechanism for website performance optimization, and he can effectively alleviate the database pressure. The caches in ASP. NET are mainly divided into:

    • Page Caching
    • Data source Caching
    • Custom Data caching
22. Doload Balancing and server bonus

Load balancing should not only be seen as a means of achieving scalability. Although it certainly improves scalability, many times it increases the performance of WEB applications because requests and users emit multiple servers.

23. Code checking and optimization via FxCop

FxCop is a code analysis tool that uses a rule-based engine to check out the non-canonical parts of your code, and you can customize your own rules to add to this engine. Some of these rules are:

    • Avoid excessive local variables
    • Avoid using private code that is not called
    • Avoid internal classes that are not instantiated
    • Avoid the use of unsealed features
    • Avoid unnecessary casts
    • Initialize a static field of a reference type inline
    • To mark an assembly with NeutralResourcesLanguageAttribute
    • Marks the member as Static, and so on.
24.asp.net PerformanceMonitoring Tools

These are tools for monitoring the performance of your code.

    • . NET Memory Analyzer
    • Red Gate ANTS Performance analysis tool
    • Fiddler
    • Performance counters

Conclusion: These are some of the performance tuning tips. Performance tuning is not a two-day work day, but an iterative process. For Web site developers, pay attention to performance issues when writing ASP. NET applications, develop good habits, improve application performance, or at least postpone the necessary hardware upgrades to reduce the cost of the site.

Improve ASP. NET Web Application performance

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.