How To Enable HTTP Compression in ASP.NET Website

This article shows how you can use HTTP compression in ASP.NET

HTTP compression reduce request / response weight and speeds up page load time. It is Webservers built in capability to make better use of bandwidth. It helps you to improve your site performance with not much efforts. For more tips on ASP.NET Website Performance Improvement Tips.

You can enable or configure HTTP Compression using below methods.

  1. Enable in IIS
  2. Command Prompt
  3. Configure in Web.config file
  4. Compress physical files using GZipStream

Enable in IIS

IIS provides below options for compression

  1. Static files only

    You can configure HTTP compression of your static files like pure HTML (.htm), text files (*.txt), office documents(*.docx, *.ppt, *.xlsx), css (*.css), javascript (*.js). The size of those files can be reduced using compression features. It helps to reduce download time and reduce required bandwidth of server.

    You do not get much benefits by using HTTP compression with image files.

  2. Dynamic Response only

    You can configure HTTP compression for content which generates dynamically. It helps more efficient utilization of server bandwidth depending on server's processor utilization.

    If server's processor utilization is already high then dynamic compression will have extra load and it can make site slower. So HTTP compression for dynamic content requires more analysis and configuration.

Configure HTTP compression

  1. Open IIS server by using inetmgr from windows Run command box.
  2. Click on sites. It shows you all the sites hosted on this server. If you want to to enable http compression for all sites you can configure settings by clicking on server name.
  3. Select the site for which you want to configure http compression under Sites node.
  4. From right pane of IIS manager click on Compression.
  5. Select Enable dynamic content compression for dynamic contents.
  6. Select Enable static Content compression for static contents.

    configure http compression using iis manager

You can also configure the details like which static files should compress and location for caching static files for all websites.

  1. Open IIS manager.
  2. Click on server name.
  3. From right pane of IIS manager click on Compression.
  4. It displays compression settings. Click on Enable static content compression

    http compression for all websites using iis manager

Enable HTTP compression using command prompt

You can enable the http compression using command prompt.

  1. Open visual studio command prompt using Run as Administrator.
  2. Go to inetsrv directory of system32. Use below command to go for right direcory.
        cd %windir%\system32\inetsrv
  3. Use Appcmd for more configuration. As this command can be execute by only Administrators group members it is required you to open command prompt as Administraors.
  4. For compression of static content use below command
        appcmd set config /section:urlCompression /minFileSizeforComp: int 
                  /directory: string /maxDiskSpace: int
  5. To enable dynamic content compression use below command at command prompt.
        appcmd set config /section:urlCompression /doDynamicCompression:True

It make changes to your machine.config file and configuration will be apply to all websites hosted on this machine.

Configure HTTP compression in Web.config

You can enable httpCompression for your application using Web.config file. You need to configure <httpCompression /> and <urlCompression /> elements under <system.webServer />

Add below configuration in web.config file.

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/json" enabled="true" />
    <add mimeType="*/*" enabled="false" />
    <add mimeType="text/*" enabled="true" />
    <add mimeType="message/*" enabled="true" />
    <add mimeType="application/x-javascript" enabled="true" />
    <add mimeType="application/atom+xml" enabled="true" />
    <add mimeType="application/xaml+xml" enabled="true" />
    <add mimeType="*/*" enabled="false" />
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
</system.webServer >


httpCompression mention which location should be used for compression. It uses the gzip dll for gzip encoding and the types of contents which should get compressed.

UrlCompression Element specifies which type of content should get compressed. As mentioned in previous points you can enable for static and/or dynamic contents of applications.

Compress physical files using GZipStream

You can compress physical aspx,css,js files and deploy it to server. It saves server's bandwidth as the file size are smaller.

Use below code

private void CompressFile(string srcPath,
                 string tarPath)
    using(FileStream srcStream 
                = File.OpenRead(srcPath))
        using(FileStream tarStream 
                    = File.OpenWrite(tarPath));
            using(GZipStream cStream = new GZipStream(srcStream,
            byte[]data = new byte[srcStream.Length];
            srcStream.Read(data, 0, data.Length);
            tarStream.Write(data, 0, data.Length);

Apart from httpCompression you can use other IIS settings to improve ASP.NET Website Performance

Speak your mind :
Leave a comment for this article on
User profile picture on

by reinanbruno at 4/11/2020 9:44:00 AM
Hello, could you help me with a question that is as follows I want to Compress physical files using GZipStream (I'm already generating the files) but on the server for some reason it does a replace on the file in the folder (C: \ inetpub \ temp \ IIS Temporary Compressed Files \ pool) for example I generated in C # the has a file called main.js that I copy to the folder (C: \ inetpub \ temp \ IIS Temporary Compressed Files \ pool) only that the statistical understanding does not take there the files it simply replaces the main.js with the file that the IIS generates automatic, how can I solve this?
post comment on
User profile picture on

by Laxmikant at 8/18/2020 8:22:00 AM
@reinanbruno, could you elabrote your question? Not clear with your expectation.
post comment on