Programming Microsoft ASP.NET MVC by Dino Esposito

Programming Microsoft ASP.NET MVC by Dino Esposito

Author:Dino Esposito
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Web / Web Programming
ISBN: 9780735680937
Publisher: Microsoft Press
Published: 2014-02-17T16:00:00+00:00


Compressing the response

These days, HTTP compression is a feature that nearly every website can afford because the number of browsers that have trouble with that is approaching zero. (Any browser released in the past 10 years recognizes the most popular compression schemes.)

In ASP.NET Web Forms, compression is commonly achieved through HTTP modules that intercept any request and compress the response. You can also turn on compression at the Internet Information Services (IIS) level. Both options work well in ASP.NET MVC, so the decision is up to you. You typically make your decision based on the parameters you need to control, including the MIME type of the resource to compress, level of compression, files to compress, and so forth.

ASP.NET MVC makes it particularly easy to implement a third option: an action-specific filter that sets things up for compression. In this way, you can control a specific URL without the need to write an HTTP module. Let’s go through another example of an action filter that adds compression to the response stream for a particular method.

In general, HTTP compression is controlled by two parameters: the Accept-Encoding header sent by the browser with each request, and the Content-Encoding header sent by the web server with each response. The Accept-Encoding header indicates that the browser is able to handle only the specified encodings—typically, gzip and deflate. The Content-Encoding header indicates the compression format of the response. Note that the Accept-Encoding header is just a request header sent by the browser; in no way should the server feel obliged to return compressed content.

When it comes to writing a compression filter, the hardest part is gaining a full understanding of what the browser is requesting. Here’s some code that works:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.