Using gzip or deflate compression in the ASP. NET Web API

Source: Internet
Author: User

Compression is a simple and effective way to reduce the size and responsiveness of response packets.

So how do I compress the ASP. I'll use a very popular library for compression/decompression called the dotnetzip library . This library can be obtained using NuGet packages

Now, we have implemented the deflate compression actionfilter.

 Public classDeflatecompressionattribute:actionfilterattribute { Public Override voidonactionexecuted (Httpactionexecutedcontext actcontext) {varContent =actContext.Response.Content; varbytes = Content = =NULL?NULL: Content. Readasbytearrayasync ().            Result; varzlibbedcontent = bytes = =NULL?New byte[0]: compressionhelper.deflatebyte (bytes); ActContext.Response.Content=Newbytearraycontent (zlibbedcontent); ActContext.Response.Content.Headers.Remove ("Content-type"); ACTCONTEXT.RESPONSE.CONTENT.HEADERS.ADD ("content-encoding","deflate"); ACTCONTEXT.RESPONSE.CONTENT.HEADERS.ADD ("Content-type","Application/json"); Base.        OnActionExecuted (Actcontext); }    } Public classCompressionhelper { Public Static byte[] Deflatebyte (byte[] str) {            if(str = =NULL)            {                return NULL; }            using(varOutput =NewMemoryStream ()) {                using (                    varCompressor =NewIonic.Zlib.DeflateStream (output, Ionic.Zlib.CompressionMode.Compress, Ionic.zl Ib. Compressionlevel.bestspeed)) {compressor. Write (str,0, str.                Length); }                returnoutput.            ToArray (); }        }    }

When using the

   [deflatecompression]        publicstring Get (int  ID)        {             return " OK "+ID;        }

Of course, if you use gzip compression, you only need to

New Ionic.Zlib.DeflateStream (instead
New Ionic.Zlib.GZipStream (, and then

ACTCONTEXT.RESPONSE.CONTENT.HEADERS.ADD ("content-encoding", "deflate");
Can, after I test,
Deflate compression is smaller than gzip-compressed code, so it is recommended to use deflate compression

Using gzip or deflate compression in the ASP. NET Web API

Related Article

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: 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.