ASP. NET performance problems

Source: Internet
Author: User

Performance is a very important issue for an application. It is also the same in Web development. I believe no one is willing to wait for 8 seconds before the computer screen to open one, this is the 8-Second Principle on the web. For the performance of an application, we should regard it as a function of the application, and should handle the performance issues during design, optimization should not be performed after the entire program is developed, because it will pay more costs.

The following describes the performance of Asp.net.
Compared with the previous version of ASP, Asp.net improves performance through engine changes. ASP is a script interpretation engine. Many codes are compiled after user requests are accepted at runtime, which causes heavy pressure on the server when many users make the same request, asp.net adopts the one-time compilation policy. The server passes the entire compilation process only when a page is requested for the first time, which greatly improves the running efficiency. However, the performance of Asp.net cannot be completely solved by engine optimization. code optimization is another important aspect.

When using Asp.net to create Web applications, we should use a three-tier structure model, which is a popular model in recent years. The three layers of the layer-3 structure are the presentation layer (the page you can see) and the business layer (implementing the business functions of the application, which is generally referred to as the component. VB or. files such as CS are compiled. dll Control), data layer (mainly involves using ado.net to access the database ). The three-layer structure can make the program more scalable, flexible, secure, platform-independent, and maintainability.

Asp.net can use C # Or VB.net advanced object-oriented languages to write logic code. When using these languages to define variables, it is required to explicitly define the types of all variables, to avoid using the late binding method of the object type, you should bind it early. Early binding is conducive to code execution and improves performance. You can add "Explicit =" true "to the page command, for example: <% @ page Language = "VB" Explicit = "true" %> in this way, the type should be specified every time the variable is declared. You can also add the strict attribute such as <% @ page Language = "VB" strict = "true" %> in the page command to avoid late binding.

Viewstate is a new feature of Asp.net. By using viewstate, you can easily maintain the control state during the round-trip to and from the server. However, using viewstate will significantly increase the file size and affect the performance. Therefore, we recommend that you disable viewstate, for example, <% @ page enableviewstate = "false" %> or separately stop the viewstate of a widget. In addition, we should select a proper method for storing sessions. There are three methods: inproc (stored in the process), StateServer (stored in the status server), and database (stored in the database ). In inproc, the performance is the best, but the scalability is relatively poor. The performance of the other two methods is not as good as that of inproc, but it has good scalability. Therefore, we should choose based on the specific situation. If you do not need to use sesion, disable such as <% @ page enablesession = "false" %>. If the read session is not modified, you can do this: <% @ page enablesession = "readonly" %> or improve the performance.

Another important way to improve performance is to use the cache technology. There are three cache mechanisms in Asp.net: Page cache, fragment cache, and data cache. The page cache puts frequently accessed pages into the cache. When users request this page again, they do not need to regenerate the page, but directly send the pages in the cache to the client, saving response time, this greatly improves the performance. The specific method is to add the outputcache command in the page code, such as: <% @ outputcache duration = "60" varybyparam = "NONE" %> in the above Code, duration refers to the cache time, set this parameter based on the actual situation of the program. varybyparam indicates that the cached content changes according to a parameter. For example, a weather query function is available on a page. When a user enters the Beijing query, the page is cached and another user queries the weather in Shanghai immediately, varybyparam plays a role to generate output results or cached errors such as Beijing, <% @ outputcache duration = "60" varybyparam = "textbox1" %> textbox1 indicates the ID of the text box of the input address. The fragment page cache is mainly used to cache the content in the user control. You can add the outputcache command to the page code of the user control, and then add the user control to the page. In some cases, a user requests a large amount of the same data. If the data is frequently read, it will waste a lot of time. In this case, the data cache technology can be applied to improve performance. Any object can be cached programmatically. In Asp.net, it is implemented through the cachel class. For example, cache ["myname"] = myname; myname = cache ["myname"];.

Web development is inseparable from databases. When operating databases, you should use stored procedures as much as possible to eliminate round-trip access to databases. When using dataset and datareader, you should also note that if the data queried from the database is only displayed on the front end and does not need to be modified, you should use datareader because datareader has better performance than dataset.

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.