Some ways to optimize ASP. NET site performance without modifying code

Source: Internet
Author: User
Tags metabase

Read Catalogue

    • Begin
    • Configure OutputCache
    • Enable Content Expiration
    • Resolving resource file Upgrade issues
    • Enable compression
    • Remove useless HttpModule
    • Other optimization options

This article describes some of the methods used to optimize ASP. NET site performance, which do not require modification of program code.
They are divided into two main areas:
1. Optimize with the extensibility of ASP.
2. Optimize IIS settings.

Back to top configuration outputcache

The method of using caching to optimize the performance of a website is estimated to be unknown. Asp. NET provides the Httpruntime.cache object to cache the data and also provides the OutputCache directive to cache the entire page output. Although the outputcache instruction is more convenient to use, it also has a very good effect, but it requires us to add such an instruction to those pages.

For pages that have been set over outputcache, the browser caches the page response after receiving a response from such a page. As long as it is within the specified cache time and the user does not have a forced flush operation, the server is not requested at all, and for requests originating from other browsers, if the cached page has been generated, it can respond to requests directly from the cache and speed up the response. Therefore, the OutputCache directive is meaningful for performance optimization (unless all page pages are updated frequently).

In the optimization phase of the site, we can use tools such as Fiddler to find some pages that will barely change , to set outputcache for them, but, according to the traditional development process, we need to do the following for each paging file:
1. Check out the paging file.
2. Add the OutputCache directive.
3. Republish the page.
4. Check in the file (you may also need a merge operation if you experience multi-branch parallelism).
the above-mentioned source code management system will complicate a simple thing , so is there a simpler way to solve this problem?

Next, this article describes a method that takes advantage of the extensibility of the ASP. config file to set the OutputCache parameter for the page. The other configuration file is an XML file with the following content:

<?xml version= "1.0" encoding= "Utf-8"? ><outputcache xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"                         xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" >    <Settings>        <setting duration= "3" FilePath = "/pages/a3.aspx"  />        <setting duration= "Ten" filepath= "/pages/a5.aspx"  />    </settings ></OutputCache>

Looking at this configuration, I think you should be able to guess how it works.

Each line of the configuration parameter specifies the parameters required for a page outputcache, the sample file for simple use only two parameters, other parameters can be supported refer to the OutputCache directive.

In order to make this configuration file valid, you need to configure the following in Web. config (for IIS7):

<system.webServer>    <modules>        <add name= "Setoutputcachemodule" type= " Websiteoptimize.setoutputcachemodule, websiteoptimize "/>    </modules></system.webServer>

Here, I registered a HttpModule, and it's all code as follows:

public class setoutputcachemodule:ihttpmodule{static Setoutputcachemodule () {//load config file        String xmlfilepath = Path.Combine (Httpruntime.appdomainapppath, "outputcache.config");    Configmanager.loadconfig (Xmlfilepath); } public void Init (HttpApplication app) {app.    PreRequestHandlerExecute + = new EventHandler (App_prerequesthandlerexecute); } void App_prerequesthandlerexecute (object sender, EventArgs e) {HttpApplication app = (HttpApplication) send        Er        dictionary<string, outputcachesetting> settings = configmanager.settings; if (settings = = null) throw new Configurationerrorsexception ("Setoutputcachemodule load config file failed.        ");        Implementation method://Find configuration parameters, if a matching request is found, set outputcache outputcachesetting setting = null; if (settings. TryGetValue (app. Request.filepath, out setting)) {setting. Setresponsecache (app.        Context); }    }

The ConfigManager class is used to read the configuration file and enables file dependency technology, which is automatically reloaded when the profile is updated:

With Autosetoutputcachemodule, we can set the OutputCache parameter for the page directly using the configuration file, without having to modify any page, is it easy to use?

Description: The MYMVC framework already supports this feature, and all relevant can be obtained from the source code of the MYMVC framework.

recommendation : Caching pages is an effective optimization method for some pages that rarely change.

Back to top enable content expiration

Each site will have some resource files (Pictures, js,css) that, relative to the ASPX page, will most likely not change over a long period of time, and IIS will not generate a Cache-control response header when responding to such resource files. In this case, the browser may cache them, perhaps restarting the request (such as after a reboot), in short, the cache behavior is not controlled and the cache time is not long enough.

Ever thought you could cache them in a browser for a long time?

In order to tell the browser to cache these files for a long time, reducing some meaningless requests (increasing page rendering speed), we can enable content expiration in IIS so that IIS can generate a Cache-control response header that explicitly tells the browser how long to cache the file.

In IIS6, this parameter is well-found:

However, in IIS7, this parameter is not easily discoverable and requires the following actions to find:
Select the site ( or site subdirectory ) node and double-click the HTTP response header

Then click on the "Set Common headers" link on the right,

This will show you:

Description:Enable Content expiration This setting can be based on the entire Web site or on a subdirectory, or a specific file.

Note: If you set "Enable Content expiration" for a subdirectory or file in IIS7, the preceding dialog box looks identical,
However, in IIS6, we can clearly see from the title bar of the dialog box what we are doing:

Sometimes it feels like IIS7 's interface is backwards!

Finally, I would like to say: can be directly for the entire site to enable content expiration, ASPX pages will not be cached!

When it comes to this, someone might think: how long should I set this expiration time?
10 minutes, 2 hours, one day, or one months?
In my opinion, the longer the better.
Perhaps someone will say: In case I want to upgrade a JS file to do, time set long, how users update it?
If you ask me this question, I can only say that your code is unreasonable (after all, you can not solve the upgrade problem), want to know why, please continue to read.

Back to top Resolve resource file upgrade issues

for some small-scale sites , resource files are typically deployed to a Web site along with program files.
In this case, you may refer to JS or CSS files in the following way:

<link type= "Text/css" href= "Aaaa.css" rel= "Stylesheet"/><script type= "Text/javascript" src= "Bbb.js" > </script>

In this case, if you use the previous "Enable Content expiration" method, then when there are js,css files that need to be upgraded, because the browser's cache has not expired, so the server will not be requested, this time the cached version will be used, so there may be a variety of strange bugs

For the bug mentioned earlier, I think the root of the problem is the way to refer to the Js,css file, which completely does not take into account the version upgrade issue, the correct method has two kinds:
1. Add a version number to the file name, like jquery, one file per version (Jquery-1.4.4.min.js).
2. Add a version number after the URL to invalidate the original URL.

The first method does not have a cache problem because each upgrade produces a new file, but the cost of maintaining a large heap of files can be large, so I recommend a second approach.

In the sample code for MYMVC, I used the following methods to refer to these resource files:

<%= htmlextension.refcssfilehtml ("/css/stylesheet.css")%><%= htmlextension.refjsfilehtml ("/js/MyPage/ Fish.js ")%>

When the page is running, the following output results are generated:

<link type= "Text/css" rel= "Stylesheet" href= "/css/stylesheet.css?_t=634642185820000000"/><script type= " Text/javascript "src="/js/mypage/fish.js?_t=634642154020000000 "></script>

The implementation code for these two tool methods is as follows (in the sample code for MYMVC):

The above method of obtaining the file version number is a relatively simple solution. Each reference place will access the last modified time of the file when generating the HTML code, which will give the disk a bit of read overhead, and if you are concerned that this implementation might affect performance, you can also add a configuration file to resolve it (self-fulfilling), such as the following structure:

<?xml version= "1.0" encoding= "Utf-8"? ><arrayoffileversion xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "                     xmlns:xsd=" Http://www.w3.org/2001/XMLSchema ">    <fileversion filepath="/js/ Jscript.js "version=" 255324 "/>    <fileversion filepath="/css/stylesheet.css "version=" 2324235 "/></ Arrayoffileversion>

If you think this kind of configuration file requires manual maintenance, not enough automation, you can also use the program to automatically maintain a list at run time, in short, the direct reference to the resource file is a direct coupling, will bring trouble to the file upgrade, We can solve this direct coupling by an external method (adding a property to the FileVersion and also changing the internal address to a CDN address).

Back to top enable compression

Compression response results are also commonly used site optimization method, because now the browser has been supported compression function, so, if the server can compress the response results, for the slow speed of users, will reduce a lot of network transmission time, the ultimate experience is the page display speed faster!

Although the IIS6 provides a compressed setup interface, the configuration is server-level:

Note: the "Application Files" here do not include ASPX, and if you need to compress the ASPX response, you need to manually modify the X:\WINDOWS\system32\inetsrv\MetaBase.xml file (refer to the Enlarged Font section):

Description: To modify the MetaBase.xml, you need to stop the IIS Admin service services.

In IIS7, we can configure the compression parameters at the server level:

Then turn the compression feature on or off in each website:

Description: MetaBase.xml is no longer used in IIS7, so we can't find the IIS6 settings. IIS7 compressed filter conditions are no longer for extensions, but mimetype rules (saved in applicationhost.config). According to the compression rules of IIS7, when we enable dynamic compression, the results of ASPX responses are compressed.

Two different compression methods:
1. Static content compression: When the server responds to a static file for the first time, it generates a compressed result and saves it to disk for reuse.
2. Dynamic content compression: "Each time" in response to the client, the compression of the response results, in memory to complete, and therefore a burden on the CPU.

Note: To "Enable dynamic content compression", you need to evaluate whether the server's CPU is capable of withstanding (observing task Manager or viewing performance counters).

Back to the top delete useless HttpModule

For a website, ASP. NET provides some of the httpmoudle may not be needed, however, if you do not manually disable them, they will actually run .
For example I will disable the following httpmoudle:

For sites that use Forms authentication, the following HttpModule can also be disabled:

Back to top other optimization options

Optimizing an ASP. NET site is a big topic, in addition to the methods described in the blog, there are also the following methods can be consulted:
1. Upgrade the server hardware configuration.
2. Use the Windows Server 2008 version of the operating system (network performance is better than 2003).
3. Optimize the operating system configuration (such as disabling services that are not required).
4. Disable debug mode.
5. The Web site uses a dedicated application pool.

Reprinted from: http://www.cnblogs.com/fish-li/archive/2012/12/23/2830301.html

Some ways to optimize ASP. NET site performance without modifying code

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.